Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Generic nodes with no type variables #11906

Conversation

HertzDevil
Copy link
Contributor

Resolves #10204.

This is purely a syntax enhancement and requires no changes outside the parser. It is already possible to obtain generic instances that have no type variables even before this PR, such as via Tuple.new; even for Generic nodes, all instantiations of NamedTuple return an empty array in #type_vars. (In contrast, resolve.type_vars returns a 1-array of the NamedTuple or Tuple itself.)

At the lexical level, a constant followed by a pair of empty parentheses can currently appear only in the following places:

lib Foo
  fun Foo() # lib fun definition
end

Foo.Foo() # lib fun call

fun bar = Bar() # global fun definition
end

annotation Ann; end

@[Ann()] # annotation with no arguments
def ann; end

The formatter removes the parentheses in all these cases. However, if we look at all empty parentheses in general, then we can see that this is not done for Calls because they could be incorrectly turned into Vars. In this PR the formatter never removes the parentheses in a Generic without type variables. Also note that in the following example:

class Foo(); end # Error: must specify at least one type var

the compiler never materializes any Generic, because the parser handles this separately in #parse_type_vars and the corresponding ClassDef / ModuleDef stores the formal parameters outside the type name.

@straight-shoota straight-shoota added this to the 1.4.0 milestone Mar 17, 2022
@straight-shoota straight-shoota merged commit 68617c8 into crystal-lang:master Mar 18, 2022
@HertzDevil HertzDevil deleted the feature/generic-empty-arguments branch March 19, 2022 11:11
beta-ziliani pushed a commit to crystal-lang/crystal-book that referenced this pull request May 26, 2022
After adding crystal-lang/crystal#11906 to Crystal 1.4, it's a good time to add definitions and examples for "using splats in generics" to the Generics section.
The last example shows the Support Generic nodes with no type variables
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.

[RFC] Allow generic instantiations with empty type vars everywhere
3 participants