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 unions in Crystal::Macros::ASTNode#is_a? #12086

Merged

Conversation

HertzDevil
Copy link
Contributor

Implements part of #11981. This now works:

macro foo(x)
  {{ x.is_a?(NumberLiteral | StringLiteral) }}
end

foo(1)    # => true
foo("a")  # => true
foo(true) # => false

It contains a rather basic implementation of the type system of the macro language, supporting unions and NoReturn. Generics will be added later. There is a lot of room for optimizing the unions, but they can only be created through explicit |s so the impact is probably low.

As a reminder, ASTNode#class_name is never affected by this type system, whether that AST node is documented or not. Thus Crystal::Program#define_macro_types should define everything that may possibly appear within the macro language (#3274 (comment)), so that things like {{ x.is_a?(ClassDef) }} do not break afterwards. Everything else (e.g. Int32) implicitly names the macro NoReturn.

Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me!

@straight-shoota straight-shoota added this to the 1.5.0 milestone Jun 8, 2022
@straight-shoota straight-shoota merged commit a7bccef into crystal-lang:master Jun 9, 2022
@HertzDevil HertzDevil deleted the feature/macro-is-a-union branch June 11, 2022 16:38
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.

3 participants