Scala 2/3中如何实现依赖类型的传递性?
在Scala 2/3中实现依赖类型的传递性
函数中的依赖类型(正常工作)
函数参数的依赖类型会在调用点自动细化,代码示例:
{ // dependent type in function def dep[B](a: Any, bs: Seq[B]): Seq[(a.type, B)] = { val result: Seq[(a.type, B)] = bs.map { b => (a: a.type) -> (b: B) } result } val ss = dep(3, Seq("a")) val ss2: Seq[(3, String)] = ss // 编译通过,a.type被解析为3.type }
原因:函数调用时,编译器会将参数a的具体类型(3.type)代入返回值的依赖类型中,实现类型细化。
类中的依赖类型(解析失败)
当依赖类型放在类成员中时,编译器无法像函数那样自动细化类型,导致类型不匹配:
{ // in case class class Dep[B](a: Any, bs: Seq[B]) { def result: Seq[(a.type, Any)] = { val result: Seq[(a.type, B)] = bs.map { b => (a: a.type) -> (b: B) } result } } object ss extends Dep(3, Seq("a")) val ss2: Seq[(3, String)] = ss.result // 编译报错 }
错误信息:
Found: Seq[((ss.a : Any), Any)] Required: Seq[((3 : Int), String)] Explanation =========== Tree: ss.result I tried to show that Seq[((ss.a : Any), Any)] conforms to Seq[((3 : Int), String)] but the comparison trace ended with `false`:
原因:类构造参数a的类型在类定义中被固定为Any,实例化后ss.a.type指向的是实例成员的类型(而非构造时的3.type),导致依赖类型无法传递到调用点。
尝试的辅助构造器方案(编译报错)
试图通过辅助构造器传递依赖类型,但Scala不允许辅助构造器引入新类型参数,导致编译失败:
{ // in case class, as auxiliary constructor case class Dep[A, B](a: A, bs: Seq[B]) { def this[B](a: Any, bs: Seq[B]) = this[a.type, B](a, bs) // 编译报错 def result: Seq[(A, Any)] = { val result: Seq[(A, B)] = bs.map { b => (a: A) -> (b: B) } result } } }
错误信息:
None of the overloaded alternatives of constructor Dep in class Dep with types [A, B](): Dep[A, B] [A, B](a: A, bs: Seq[B]): Dep[A, B] match arguments (Null)
解决方案:显式绑定类型参数
要让类支持依赖类型的传递性,需将构造参数的类型显式提取为类的类型参数,而非依赖路径依赖类型a.type:
Scala 2/3通用实现
class Dep[A, B](val a: A, val bs: Seq[B]) { def result: Seq[(A, B)] = bs.map(b => a -> b) } // 实例化时编译器自动推断A=3.type,B=String object ss extends Dep(3, Seq("a")) val ss2: Seq[(3.type, String)] = ss.result // 编译通过
这样类的类型参数A会在实例化时被细化为3.type,result的类型也会随之确定,实现和函数一致的依赖类型传递性。
Scala 3优化写法(利用细化类型)
Scala 3支持更简洁的细化类型语法,可以直接约束A为单例类型:
class Dep[A <: Singleton, B](val a: A, val bs: Seq[B]) { def result: Seq[(A, B)] = bs.map(b => a -> b) } val ss = new Dep(3, Seq("a")) val ss2: Seq[(3, String)] = ss.result // 编译通过
通过A <: Singleton确保A是单例类型,进一步强化类型约束。
内容的提问来源于stack exchange,提问作者tribbloid
相关产品推荐
相关产品推荐

