为什么scala不会将这种类型的lambda与底层类型统一起来?
发布时间:2020-12-16 18:58:14 所属栏目:安全 来源:网络整理
导读:trait A { type T def test(t: T): Unit}case class B[S : A](a: S,t : S#T) { def test() = a.test(t) // Error: type mismatch; // found : B.this.t.type (with underlying type S#T) // required: B.this.a.T} 我错误地期望上面的编译?我的代码可以修复
trait A { type T def test(t: T): Unit } case class B[S <: A](a: S,t : S#T) { def test() = a.test(t) // Error: type mismatch; // found : B.this.t.type (with underlying type S#T) // required: B.this.a.T } 我错误地期望上面的编译?我的代码可以修复吗? 解决方法
编译器没有足够的证据表明S#T可以在具体实例中用作测试参数.
考虑弱化scala编译器的这个低级示例 trait A2 extends A{ type T <: AnyRef } class A3 extends A2{ override type T = Integer def test(t: Integer): Unit = println(t * 2) } 所以B [A2]应该接受A3的实例以及任何< ;: AnyRef,而A3需要完全Integer用于自己的测试实现 您可以在B的定义中捕获具体类型,以确保将使用哪种类型 case class B[S <: A,ST](a: S {type T = ST},t: ST) { def test() = a.test(t) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |