summaryrefslogtreecommitdiff
path: root/test/files/run/MeterCaseClass.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2012-03-07 14:13:03 +0100
committerMartin Odersky <odersky@gmail.com>2012-03-07 15:23:17 +0100
commit54e284d669418ebc6445bd0ec66804b9067f6dd3 (patch)
tree34bca4c9ad11d324e574f157c156b39f8aeb72ea /test/files/run/MeterCaseClass.scala
parente9a1207eedfd023a7ffe0723e4c8989d341e2f65 (diff)
downloadscala-54e284d669418ebc6445bd0ec66804b9067f6dd3.tar.gz
scala-54e284d669418ebc6445bd0ec66804b9067f6dd3.tar.bz2
scala-54e284d669418ebc6445bd0ec66804b9067f6dd3.zip
Allows case classes as value classes
Diffstat (limited to 'test/files/run/MeterCaseClass.scala')
-rw-r--r--test/files/run/MeterCaseClass.scala99
1 files changed, 99 insertions, 0 deletions
diff --git a/test/files/run/MeterCaseClass.scala b/test/files/run/MeterCaseClass.scala
new file mode 100644
index 0000000000..4f082b5252
--- /dev/null
+++ b/test/files/run/MeterCaseClass.scala
@@ -0,0 +1,99 @@
+package a {
+ case class Meter(underlying: Double) extends AnyVal with _root_.b.Printable {
+ def + (other: Meter): Meter =
+ new Meter(this.underlying + other.underlying)
+ def / (other: Meter): Double = this.underlying / other.underlying
+ def / (factor: Double): Meter = new Meter(this.underlying / factor)
+ def < (other: Meter): Boolean = this.underlying < other.underlying
+ def toFoot: Foot = new Foot(this.underlying * 0.3048)
+ override def print = { Console.print(">>>"); super.print; proprint }
+ }
+
+ object Meter extends (Double => Meter) {
+
+ implicit val boxings = new BoxingConversions[Meter, Double] {
+ def box(x: Double) = new Meter(x)
+ def unbox(m: Meter) = m.underlying
+ }
+ }
+
+ class Foot(val unbox: Double) extends AnyVal {
+ def + (other: Foot): Foot =
+ new Foot(this.unbox + other.unbox)
+ override def toString = unbox.toString+"ft"
+ }
+ object Foot {
+ implicit val boxings = new BoxingConversions[Foot, Double] {
+ def box(x: Double) = new Foot(x)
+ def unbox(m: Foot) = m.unbox
+ }
+ }
+
+}
+package b {
+ trait Printable extends Any {
+ def print: Unit = Console.print(this)
+ protected def proprint = Console.print("<<<")
+ }
+}
+import a._
+import _root_.b._
+object Test extends App {
+
+ {
+ val x: Meter = new Meter(1)
+ val a: Object = x.asInstanceOf[Object]
+ val y: Meter = a.asInstanceOf[Meter]
+
+ val u: Double = 1
+ val b: Object = u.asInstanceOf[Object]
+ val v: Double = b.asInstanceOf[Double]
+ }
+
+ val x = new Meter(1)
+ val y = x
+ println((x + x) / x)
+ println((x + x) / 0.5)
+ println((x < x).toString)
+ println("x.isInstanceOf[Meter]: "+x.isInstanceOf[Meter])
+
+
+ println("x.hashCode: "+x.hashCode)
+ println("x == 1: "+(x == 1))
+ println("x == y: "+(x == y))
+ assert(x.hashCode == (1.0).hashCode)
+
+ val a: Any = x
+ val b: Any = y
+ println("a == b: "+(a == b))
+
+ { println("testing native arrays")
+ val arr = Array(x, y + x)
+ println(arr.deep)
+ def foo[T <: Printable](x: Array[T]) {
+ for (i <- 0 until x.length) { x(i).print; println(" "+x(i)) }
+ }
+ val m = arr(0)
+ println(m)
+ foo(arr)
+ }
+
+ { println("testing wrapped arrays")
+ import collection.mutable.FlatArray
+ val arr = FlatArray(x, y + x)
+ println(arr)
+ def foo(x: FlatArray[Meter]) {
+ for (i <- 0 until x.length) { x(i).print; println(" "+x(i)) }
+ }
+ val m = arr(0)
+ println(m)
+ foo(arr)
+ val ys: Seq[Meter] = arr map (_ + new Meter(1))
+ println(ys)
+ val zs = arr map (_ / Meter(1))
+ println(zs)
+ val fs = arr map (_.toFoot)
+ println(fs)
+ }
+
+}