scala type alias - how to have a type that represe

2019-09-04 04:38发布

问题:

Is it possible to define a type alias which represent mutliple data types?

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

e.g. I need a suptertype lets say DataDimension that represent only above three types. so I can do following:

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
  }

}

But following should give compile error. Currently it works.

class stringDimensionWorker extends AbstractDataWorker[String] {

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

    "hello"
  }

}

回答1:

You can make them real classes extending a single trait instead of type aliases:

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] {
  // ...
}

Or you can make a typeclass with implicit implementations for those type aliases. Then make AbstractDataWorker an abstract class, and add a context bound for this typeclass to its type argument:

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] {
  // ...
}


标签: scala