Scala 中的类型相等 [英] Type equality in Scala

查看:52
本文介绍了Scala 中的类型相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一小段代码:

class Foo[A] {
  def foo[B](param: SomeClass[B]) {
  //
  }
}

现在,在 foo 中,我该怎么做:
1) 验证 B 是否与 A 的类型相同?
2) 验证 B 是否是 A 的子类型?

Now, inside foo, how do I:
1) verify if B is the same type as A?
2) verify if B is a subtype of A?

推荐答案

你需要隐式类型证据,<:< 用于子类型检查,=:= 用于相同类型的检查.请参阅此问题的答案.

You need implicit type evidences, <:< for subtype check and =:= for same type check. See the answers for this question.

这篇关于Scala 中的类型相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆