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

No way to use alias of NamedTuple in macros #4301

Closed
relonger opened this issue Apr 16, 2017 · 6 comments · Fixed by #4995
Closed

No way to use alias of NamedTuple in macros #4301

relonger opened this issue Apr 16, 2017 · 6 comments · Fixed by #4995

Comments

@relonger
Copy link

crystal -v
Crystal 0.21.1 (2017-03-14) LLVM 4.0.0

alias TT = {a: Int32, b: String}

{% for k in TT.keys %}
  puts "{{k.name}}"
{% end %}

Generate errors:

undefined method 'keys' for TypeNode of type TT (must be a named tuple type)

{% for k in TT.keys %}
               ^~~~

But TT is exactly the NamedTuple (the alias of it). Tried a lot of combinations but nothing works - I can't access info about NamedTuple type after its declaration.

@asterite
Copy link
Member

TT is an alias type, not a NamedTuple type.

The question is whether we want to remove the alias automatically here, or with an explicit operation.

@bew
Copy link
Contributor

bew commented Apr 18, 2017

alias TT = {a: Int32, b: Char}
{% puts TT.class_name %}

Says it's a TypeNode, maybe we could add a macro method to TypeNode like TypeNode#alias? that would return the alias'd TypeNode if it's an alias type, and nil otherwise, so we could do something like:

alias TT = {a: Int32, b: Char}
{% puts TT.alias?.keys %} # => a, b

@relonger
Copy link
Author

The question is whether we want to remove the alias automatically here, or with an explicit operation.

Either methods are suitable, but neither work now. There is now Alias type in Macro module.

@bew
Copy link
Contributor

bew commented Apr 22, 2017

The question is whether we want to remove the alias automatically here, or with an explicit operation.

I'd say remove the alias automatically so TypeNode manipulations is transparent, but allow to check if it's an alias with .alias?:

alias TT = {a: Int32, b: Char}

# type manipulation
{% puts TT.class_name %} # => NamedTuple
{% puts TT.keys %} # => [a, b]

# alias?
{% puts TT.alias? %} # => AliasType (or something similar)
{% puts TT.alias?.name %} # => TT

@veelenga
Copy link
Contributor

veelenga commented May 3, 2017

Another case is union types:

alias TT = Int32 | String

{% puts TT.union? %} #=> false
{% puts TT.union_types %}  #=> undefined method 'union_types' for TypeNode of type TT (must be a union type)

@ozra
Copy link
Contributor

ozra commented May 4, 2017

I'm in favour of explicit operation; TT is an Alias-node. It shouldn't disappear in thin air!? Just a method to get the "target-node"/"aliased node" from the alias-node and then get down to business.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants