Created
September 29, 2011 01:06
-
-
Save mepcotterell/1249744 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyNumContainerA [T : Numeric] (d: T) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyNumContainerB [T <% Numeric] (d: T) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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