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 types in Crystal::Macros::Type#is_a? #12118

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

HertzDevil
Copy link
Contributor

Resolves #11981. With this, all the type names used in the macro method docs will actually mean something (even if they aren't used yet).

This PR makes the following macro types generic:

class ArrayLiteral(T); end      # T = union of element types
class HashLiteral(K, V); end    # K = union of key types, V = union of value types
class TupleLiteral(T); end      # T = union of element types
class NamedTupleLiteral(V); end # V = union of value types

But see #11981 (comment) on whether the last two should become variadic.

The second commit forbids all of the short forms that are previously allowed in arguments to the macro #is_a?. As mentioned in #11981, they never worked in the first place, because no AST node's #class_name ever contains those short forms or the type names they expand to:

x = 1
x.is_a?(NumberLiteral?)   # => false
nil.is_a?(NumberLiteral?) # => false

# after #12086
x = 1
x.is_a?(NumberLiteral?)   # => true
nil.is_a?(NumberLiteral?) # => false

# after this PR
x = 1
x.is_a?(NumberLiteral?)   # Error: cannot use `?` in macro types
nil.is_a?(NumberLiteral?) # Error: cannot use `?` in macro types

A corresponding PR for the reference manual will follow shortly.

@straight-shoota straight-shoota added this to the 1.6.0 milestone Sep 10, 2022
@HertzDevil HertzDevil marked this pull request as draft September 14, 2022 18:59
@HertzDevil HertzDevil removed this from the 1.6.0 milestone Sep 21, 2022
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.

The type system of AST node macro types
2 participants