From edaf481155a550c2b5199de6702c7cbdc2007d58 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 5 Mar 2012 16:05:07 +0100 Subject: new and updated test cases for value classes. --- test/files/neg/t900.check | 4 ++-- test/files/neg/valueclasses.scala | 18 ++++++++++++++++++ test/files/run/genericValueClass.check | 2 ++ test/files/run/genericValueClass.scala | 17 +++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 test/files/run/genericValueClass.check create mode 100644 test/files/run/genericValueClass.scala (limited to 'test/files') diff --git a/test/files/neg/t900.check b/test/files/neg/t900.check index cede26258b..047094ad6e 100644 --- a/test/files/neg/t900.check +++ b/test/files/neg/t900.check @@ -2,8 +2,8 @@ t900.scala:4: error: type mismatch; found : Foo.this.x.type (with underlying type Foo.this.bar) required: AnyRef Note that implicit conversions are not applicable because they are ambiguous: - both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A] - and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A] + both method any2stringadd in object Predef of type (x: Any)scala.runtime.StringAdd + and method any2stringfmt in object Predef of type (x: Any)scala.runtime.StringFormat are possible conversion functions from Foo.this.x.type to AnyRef def break(): x.type ^ diff --git a/test/files/neg/valueclasses.scala b/test/files/neg/valueclasses.scala index 5979f6f684..e405d95489 100644 --- a/test/files/neg/valueclasses.scala +++ b/test/files/neg/valueclasses.scala @@ -31,6 +31,24 @@ class V12[@specialized T, U](val x: (T, U)) extends AnyVal // fail class V13(x: Int) extends AnyVal // fail +package time { +object TOD { + final val SecondsPerDay = 86400 + + def apply(seconds: Int) = { + val n = seconds % SecondsPerDay + new TOD(if (n >= 0) n else n + SecondsPerDay) + } +} + +final class TOD private (val secondsOfDay: Int) extends AnyVal { // should fail with private constructor + def hours = secondsOfDay / 3600 + def minutes = (secondsOfDay / 60) % 60 + def seconds = secondsOfDay % 60 + + override def toString = "%02d:%02d:%02d".format(hours, minutes, seconds) +} +} diff --git a/test/files/run/genericValueClass.check b/test/files/run/genericValueClass.check new file mode 100644 index 0000000000..ec3a41a6a9 --- /dev/null +++ b/test/files/run/genericValueClass.check @@ -0,0 +1,2 @@ +(1,abc) +(2,def) diff --git a/test/files/run/genericValueClass.scala b/test/files/run/genericValueClass.scala new file mode 100644 index 0000000000..68162bb685 --- /dev/null +++ b/test/files/run/genericValueClass.scala @@ -0,0 +1,17 @@ +final class ArrowAssoc[A](val __leftOfArrow: A) extends AnyVal { + @inline def -> [B](y: B): Tuple2[A, B] = Tuple2(__leftOfArrow, y) + def →[B](y: B): Tuple2[A, B] = ->(y) +} + +object Test extends App { + { + @inline implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] = new ArrowAssoc(x) + val x = 1 -> "abc" + println(x) + } + + { + val y = 2 -> "def" + println(y) + } +} -- cgit v1.2.3