Skip to content

Instantly share code, notes, and snippets.

@soc
Created February 5, 2012 15:33
Show Gist options
  • Select an option

  • Save soc/1746097 to your computer and use it in GitHub Desktop.

Select an option

Save soc/1746097 to your computer and use it in GitHub Desktop.
Problem with implicits ...
object UnitsEx {
case class Quantity[M <: MInt, T: Numeric](value: T) {
private val num = implicitly[Numeric[T]]
def *[M2 <: MInt](m: Quantity[M2, T]) = Quantity[M, T](num.times(value, m.value))
}
implicit def measure[T: Numeric](v: T): Quantity[_0, T] = Quantity[_0, T](v)
implicit def numericToQuantity[T: Numeric](v: T): QuantityConstructor[T] = new QuantityConstructor[T](v)
class QuantityConstructor[T: Numeric](v: T) {
def m = Quantity[_1, T](v)
}
sealed trait MInt
final class _0 extends MInt
final class _1 extends MInt
}
import UnitsEx._
(1 m) * 1 // Works
1 * (1 m) // Doesn't work:
/*
<console>:1: error: overloaded method value * with alternatives:
(x: Double)Double <and>
(x: Float)Float <and>
(x: Long)Long <and>
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int
cannot be applied to (UnitsEx.Quantity[UnitsEx._1,Int])
1 * (1 m)
^
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment