Scala - case 类 继承类型
Scala - case classes inheriting types
有没有办法让 case class 接受在它混合的特征中定义的类型?
当我尝试用普通 classes 做什么时,它失败了:
trait myTypes{
type aType = Array[String]
}
abstract class ParentClass extends myTypes{
//no issue here
val a:aType = Array.fill[String](7)("Hello")
}
//error: not found: type aType
case class ChildClass(arg:aType) extends ParentClass
//error: not found: type aType
case class ChildClass2(arg:aType) extends myTypes
不确定为什么 Scala 会选择这种方式,但如果能帮助我避免这个恼人的错误,我将不胜感激。
发生这种情况是因为类型别名超出范围:
// error: not found: type Bar
class Foo(val bar: Bar) { type Bar = String }
改为尝试:
class Foo(val bar: Foo#Bar) { type Bar = String }
有没有办法让 case class 接受在它混合的特征中定义的类型? 当我尝试用普通 classes 做什么时,它失败了:
trait myTypes{
type aType = Array[String]
}
abstract class ParentClass extends myTypes{
//no issue here
val a:aType = Array.fill[String](7)("Hello")
}
//error: not found: type aType
case class ChildClass(arg:aType) extends ParentClass
//error: not found: type aType
case class ChildClass2(arg:aType) extends myTypes
不确定为什么 Scala 会选择这种方式,但如果能帮助我避免这个恼人的错误,我将不胜感激。
发生这种情况是因为类型别名超出范围:
// error: not found: type Bar
class Foo(val bar: Bar) { type Bar = String }
改为尝试:
class Foo(val bar: Foo#Bar) { type Bar = String }