首页 文章

迭代宏中的联合类型

提问于
浏览
1

有没有办法迭代宏中的联合类型中的类型?这是我的尝试:

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

2 回答

  • 1

    看起来目前无法在宏中使用复杂的别名类型 . 这是一个错误,请举例说明crystal-lang/crystal#4301 .

  • 0

    也许 typeof 方法会帮助你

    alias MyType = Int32 | String
    
    puts typeof(MyType) # => (Int32 | String):Class
    

    try here

相关问题