estolua
estolua

Reputation: 676

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?

Upvotes: 2

Views: 169

Answers (2)

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179119

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.

Upvotes: 2

wingedsubmariner
wingedsubmariner

Reputation: 13667

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)

Upvotes: 2

Related Questions