简体   繁体   中英

How to preserve argument type of polymorphic function in return type

Given a polymorphic function, how to match the polymorphic argument and return a value of the same type without resorting to explicit casts?

sealed trait Data
case class DString(s: String) extends Data
case class DInt(n: Int) extends Data

def double[D <: Data](d: D): D = d match {
  case DString(s) => DString(s ++ s)
  case DInt(n) => DInt(n + n)
}

This produces type mismatches (found DString / DInt , required D ). Why does the type system not accept this when the input type clearly equals the output type?

This seems like a reformulation of a problem that pops up time and again on StackOverflow , which is usually solved using F-bounded polymorphism. The only difference is that you want to preserve the type you're working on in a function that's not defined inside the trait.

So, what I'd do is move that double method inside the Data trait and use an abstract type member:

sealed trait Data {
  type Self <: Data
  def double: Self
}

case class DString(s: String) extends Data {
  type Self = DString
  def double = DString(s ++ s)
}

case class DInt(n: Int) extends Data {
  type Self = DInt
  def double = DInt(n + n)
}

Otherwise, I'm not aware of a solution.

This may be a legitimate place to use method overloading:

def double(ds: DString) = DString(ds.s ++ ds.s)
def double(di: DInt) = DInt(di.n + di.n)

You can also use type classes:

abstract class DataDoubler[A <: Data] {
  def double(a: A): A
}

implicit object DStringDoubler extends DataDoubler[DString] {
  def double(ds: DString) = DString(ds.s ++ ds.s)
}

implicit object DIntDoubler extends DataDoubler[DInt] {
  def double(di: DInt) = DInt(di.n + di.n)
}

def double[A <: Data](a: A)(implicit dd: DataDoubler[A]): A = dd.double(a)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM