在宏中迭代联合类型
Iterate union type in a macro
有没有办法在宏的联合类型中迭代类型?这是我的尝试:
alias MyType = Int32 | String
{% for type in MyType.union_types %} #=> undefined method 'union_types' for TypeNode of type MyType (must be a union type)
...
{% end %}
我尝试使用 TypeNode#union_types 方法,但似乎 MyType
根本不被视为联合类型:
{% puts MyType.union? %} #=> false
也许typeof
方法能帮到你
alias MyType = Int32 | String
puts typeof(MyType) # => (Int32 | String):Class
看起来目前无法在宏中使用复杂的别名类型。这是一个错误,请用你的例子撞 crystal-lang/crystal#4301。
有没有办法在宏的联合类型中迭代类型?这是我的尝试:
alias MyType = Int32 | String
{% for type in MyType.union_types %} #=> undefined method 'union_types' for TypeNode of type MyType (must be a union type)
...
{% end %}
我尝试使用 TypeNode#union_types 方法,但似乎 MyType
根本不被视为联合类型:
{% puts MyType.union? %} #=> false
也许typeof
方法能帮到你
alias MyType = Int32 | String
puts typeof(MyType) # => (Int32 | String):Class
看起来目前无法在宏中使用复杂的别名类型。这是一个错误,请用你的例子撞 crystal-lang/crystal#4301。