Skip to content

Instantly share code, notes, and snippets.

@mepcotterell
Created September 29, 2011 01:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mepcotterell/1249744 to your computer and use it in GitHub Desktop.
Save mepcotterell/1249744 to your computer and use it in GitHub Desktop.
case class MyNum (val d: Double) extends Numeric [MyNum]
{
def fromInt (n: Int) = MyNum (n)
def + (rhs: MyNum) = plus(this, rhs)
def plus (x: MyNum, y: MyNum): MyNum = MyNum (x.d + y.d)
def minus (x: MyNum, y: MyNum): MyNum = MyNum (x.d - y.d)
def times (x: MyNum, y: MyNum): MyNum = MyNum (x.d * y.d)
def negate (x: MyNum): MyNum = MyNum (-x.d)
def toInt (x: MyNum): Int = x.d.toInt
def toLong (x: MyNum): Long = x.d.toLong
def toFloat (x: MyNum): Float = x.d.toFloat
def toDouble (x: MyNum): Double = x.d.toDouble
def compare (x: MyNum, y: MyNum) = x.d compare y.d
override def toString = "%s".format(d)
}
class MyNumContainerA [T : Numeric] (d: T)
class MyNumContainerB [T <% Numeric] (d: T)
object MyNumTest.scala extends App {
val a = MyNum (1.0)
val b = MyNum (3.0)
val c = a + b // works fine
//val d = new MyNumContainerA (a) // does not compile
val e = new MyNumContainerB (a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment