scala类型别名 - 如何使用表示多种数据类型的类型

时间:2015-09-04 21:03:51

标签: scala

是否可以定义表示多个数据类型的类型别名?

package object scala {
  type SingleDimension = Double
  type MultiDimensionMap = Map[String, Double]
  type MultiDimensionList = List[Tuple2[String, Double]]
}

e.g。我需要一个suptertype让我们说DataDimension只代表三种类型。所以我可以这样做:

trait AbstractDataWorker[T] {

  def formula(d: Double): T
}


class multiDimensionWorker extends AbstractDataWorker[MultiDimensionMap] {

  type T = MultiDimensionMap
  override def formula(d: Double): MultiDimensionMap = {

    Map[String, Double]()
  }

}

class singleDimensionWorker extends AbstractDataWorker[SingleDimension] {

  type T = SingleDimension
  override def formula(d: Double): SingleDimension = {
    2.0
  }

}

但是下面应该给出编译错误。目前它有效。

class stringDimensionWorker extends AbstractDataWorker[String] {

  type T = String
  override def formula(d: Double): String = {

    "hello"
  }

}

1 个答案:

答案 0 :(得分:5)

您可以使它们成为扩展单个特征而不是类型别名的真实类:

sealed trait DimensionLike
case class SingleDimension(value: Double) extends DimensionLike
case class MultiDimensionMap(value: Map[String, Double]) extends DimensionLike
case class MultiDinmensionList(value: List[(String, Double)]) extends DimensionLike

abstract class AbstractDataWorker[T <: DimensionLike] {
  def formula(d: Double): T
}

class MultiDimensionWorker extends AbstractDataWorker[MultiDimensionMap] {
  // ...
}

或者你可以使用这些类型别名的隐式实现创建一个类型类。然后将AbstractDataWorker设为abstract class,并将此类型类的上下文绑定添加到其类型参数中:

type SingleDimension = Double
type MultiDimensionMap = Map[String, Double]
type MultiDimensionList = List[(String, Double)]

sealed trait IsDimensionLike[T]
object IsDimensionLike {
  implicit object singleDimension extends IsDimensionLike[SingleDimension]
  implicit object multiDimensionMap extends IsDimensionLike[MultiDimensionMap]
  implicit object multiDimensionList extends IsDimensionLike[MultiDimensionList]
}

abstract class AbstractDataWorker[T : IsDimensionLike] {
  def formula(d: Double): T
}

class MultiDimensionWorker extends AbstractDataWorker[MultiDimensionMap] {
  // ...
}