【问题标题】:Crystal lang : Type arithmetic, belongs to a Union?Crystal lang : 类型算术,属于一个Union?
【发布时间】:2018-06-29 06:40:13
【问题描述】:

我正在尝试实现一个功能:

def foo(t : Class)
    if t in Int::Signed
        # ...
    end
end

但是如何实现t in Int::Signed?在哪里Int::Signed

我知道is_a?(Int::Signed),但这里的参数是Type 类型。 谢谢。

【问题讨论】:

  • 我也知道case when,但是我觉得太长太丑了。
  • @Amadan 抱歉,我是Class。我已经纠正了我的问题。但是typeof(1_64) == Int::Signed 返回false

标签: types crystal-lang


【解决方案1】:
def foo(t : Class)
  if t < Int::Signed
    # ...
  end
end

Class#&lt; 仅在 Crystal 0.25 中添加,如果我没记错的话,如果它不适合你,请确保更新。还有Class#&lt;= 会为Int::Signed &lt;= Int::Signed 返回true

【讨论】:

  • 谢谢!我还在用0.24.2,没有这个方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多