Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context bounds and pattern matching in scala

Tags:

scala

I am experimenting with context bounds in scala and I don't find a way to make the either of these two functions typecheck:

abstract class Expr
case class Val[T: Numeric](v: T) extends Expr
case object Other extends Expr

val e1 = Val(1)
val e2 = Val(2)

def addExp1(e1: Expr, e2: Expr): Expr = (e1, e2) match {
  case (Val(v1), Val(v2)) => Val(v1+v2)
  case _ => Other
}

def addExp2[T: Numeric](e1: Expr, e2: Expr): Expr = (e1, e2) match {
  case (Val(v1: T), Val(v2: T)) => Val(v1+v2)
  case _ => Other
}

In the case of addExp1, I can understand that the compiler has no information at the function definition point to know that the arguments of Val are Numeric and thus have a + method. It just matches Any as the type of v1.

In the case of addExp2, how can I force the bound in the pattern? The type is "erased"...T annotation is eliminated by erasure...

What I would dream of having is a single point to put the bound, ideally at the definition of the Val class.

like image 490
Pierre Boulet Avatar asked Dec 01 '25 04:12

Pierre Boulet


1 Answers

The problem is that when you pattern match, the two instances of Val can have different type parameters, say Val[T1] and Val[T2].

You can fix that as suggested by @rjsvaljean, and add import Numeric.Implicits._ to use the nice operator notation.

like image 162
Iulian Dragos Avatar answered Dec 02 '25 19:12

Iulian Dragos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!