不可变泛型的特定于类型的函数 类
Type-specific functions for immutable generic classes
假设我有一个 Container_Generic[T]
通用 class,并且我希望仅当 T
为 Double
时才能使用特定的 functions
。
有没有办法做到这一点而不需要可变性或创建子classes?
我问的原因是因为 sub-classes 需要很多不必要的代码重复。请参见下面的示例:
class Container_Generic[T](x: Seq[T]){
def map(f: T => T): Container_Generic[T] = new Container_Generic[T](x map f)
// def max: Double = x.max //<-Impossible to implement unless T pre-specified
}
class Container_Double_works(x: Seq[Double]) extends Container_Generic[Double](x){
override def map(f: Double => Double): Container_Double_works = new Container_Double_works(x map f)
def max: Double = x.max
}
class Container_Double_doesntWork(x: Seq[Double]) extends Container_Generic[Double](x){
def max: Double = x.max
}
// Work
val C_base = new Container_Generic[Int](Seq(1)).map(_*2) // : Container_Generic[Int]
val C_double = new Container_Double_works(Seq(1)).map(_*2) // : Container_Double_works
// Don't work correctly: No longer same class
val C_double_doesntWork = new Container_Double_doesntWork(Seq(1)).map(_*2) // : Container_Generic
我对 Scala 还是个新手,所以我可能采用了错误的方法,或者可能只是不知道正确的术语。
Scala implicit 几乎可以做任何事情
class Container_Generic[T](val x: Seq[T]) {
def map(f: T => T): Container_Generic[T] = new Container_Generic[T](x map f)
}
implicit class WithMax(val c: Container_Generic[Double]) {
def max: Double = c.x.max
}
val cDouble: Container_Generic[Double] = new Container_Generic(Seq(1.12))
val cString: Container_Generic[String] = new Container_Generic(Seq("12"))
println(cDouble.max)
//won't compile
// println(cString.max)
假设我有一个 Container_Generic[T]
通用 class,并且我希望仅当 T
为 Double
时才能使用特定的 functions
。
有没有办法做到这一点而不需要可变性或创建子classes?
我问的原因是因为 sub-classes 需要很多不必要的代码重复。请参见下面的示例:
class Container_Generic[T](x: Seq[T]){
def map(f: T => T): Container_Generic[T] = new Container_Generic[T](x map f)
// def max: Double = x.max //<-Impossible to implement unless T pre-specified
}
class Container_Double_works(x: Seq[Double]) extends Container_Generic[Double](x){
override def map(f: Double => Double): Container_Double_works = new Container_Double_works(x map f)
def max: Double = x.max
}
class Container_Double_doesntWork(x: Seq[Double]) extends Container_Generic[Double](x){
def max: Double = x.max
}
// Work
val C_base = new Container_Generic[Int](Seq(1)).map(_*2) // : Container_Generic[Int]
val C_double = new Container_Double_works(Seq(1)).map(_*2) // : Container_Double_works
// Don't work correctly: No longer same class
val C_double_doesntWork = new Container_Double_doesntWork(Seq(1)).map(_*2) // : Container_Generic
我对 Scala 还是个新手,所以我可能采用了错误的方法,或者可能只是不知道正确的术语。
Scala implicit 几乎可以做任何事情
class Container_Generic[T](val x: Seq[T]) {
def map(f: T => T): Container_Generic[T] = new Container_Generic[T](x map f)
}
implicit class WithMax(val c: Container_Generic[Double]) {
def max: Double = c.x.max
}
val cDouble: Container_Generic[Double] = new Container_Generic(Seq(1.12))
val cString: Container_Generic[String] = new Container_Generic(Seq("12"))
println(cDouble.max)
//won't compile
// println(cString.max)