Skip to content

Conversation

straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented Aug 26, 2025

Several ASTNode implementations have a concept of arguments: Call, Def, FunDef, Macro, ProcPointer, Block, Annotation.
Arguments are often separated into different properties, such as args, named_args and block_args on Call. In order to know whether the node has any args, we need to check if either of them is present. This is a common pattern throughout the compiler code base. The tricky thing is to remember for each node type, which properties need to be checked. So in some cases we're not checking for all conditions. Whether this is a relevant omission, depends on the exact circumstance.

This patch introduces the #has_any_args? method on all node types with arguments. Even those where it's really just an args property (Block and ProcPointer). This makes it very convenient to check for the absence of any arguments without having to worry about the node type's data layout and potentially forgetting something.

@Sija
Copy link
Contributor

Sija commented Aug 26, 2025

I like the idea of code simplification 💯 One thing that stood out for me was the negated naming scheme. I'd expect such a method to be called has_args? rather than, as it's more natural to expect things and not negate them.

@straight-shoota
Copy link
Member Author

straight-shoota commented Aug 27, 2025

I think I chose the name because the existing uses are testing for absence.
But you're right, a positive spin would make more sense.

I'm wondering though about the name, because has_args? could be understood as synonymous with args.present?. But it's more than that on most types. (the same argument goes for has_no_args? of course).

Maybe #has_any_args? is a bit more expressive? Or just #any_args?...

@ysbaddaden
Copy link
Contributor

I also think any is more explicit to the intent: is there any named/splat/block/regular arg?

@straight-shoota
Copy link
Member Author

A more technical term would be #nullary?. That still checks for absence, but it's a positive spin.
It might not be as obvious as #has_any_arg? though.

@ysbaddaden
Copy link
Contributor

ysbaddaden commented Aug 28, 2025

Oh, I learnt a new word! #nullary? sounds perfect 👌

@HertzDevil
Copy link
Contributor

"Nullary" is a semantic property, but a "nullary" super or previous_def Call may have arguments if it has no parentheses. Similarly, a "nullary" ProcPointer takes arguments if it is itself an argument to a lib fun. So I think the previous naming is more appropriate, because what we are querying here is in fact a syntactic property.

@straight-shoota straight-shoota changed the title Add #has_no_args? method for ASTNodes Add #has_any_args? method for ASTNodes Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants