New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compiler error when using tagged types and case classes #5183
Comments
Imported From: https://issues.scala-lang.org/browse/SI-5183?orig=1 |
@dlwh said: |
@adriaanm said: |
Taylor Leese (tleese22) said: |
@adriaanm said: Welcome to Scala version 2.10.0-RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait Day
defined trait Day
scala> case class Test1(d: Int with Day)
error: type mismatch;
found : Double
required: AnyRef
Note: an implicit exists from scala.Double => java.lang.Double, but
methods inherited from Object are rendered ambiguous. This is to avoid
a blanket implicit which would convert any scala.Double to any AnyRef.
You may wish to use a type ascription: `x: java.lang.Double`. |
@retronym said: scala210
Welcome to Scala version 2.10.0-20121024-085118-2c554249fd (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class User
defined class User
scala> class Checkin
defined class Checkin
scala>
| type Tagged[U] = { type Tag = U }
defined type alias Tagged
scala> type @@[T, U] = T with Tagged[U] // Thanks to @retronym for suggesting this type alias
defined type alias $at$at
scala>
| class Tagger[U] {
| def apply[T](t : T) : T @@ U = t.asInstanceOf[T @@ U]
| }
defined class Tagger
scala> def tag[U] = new Tagger[U]
tag: [U]=> Tagger[U]
scala>
| // Manual specialization needed here ... specializing apply above doesn't help
scala> def tag[U](i : Int) : Int @@ U = i.asInstanceOf[Int @@ U]
tag: [U](i: Int)@@[Int,U]
scala> def tag[U](l : Long) : Long @@ U = l.asInstanceOf[Long @@ U]
tag: [U](l: Long)@@[Long,U]
scala> def tag[U](d : Double) : Double @@ U = d.asInstanceOf[Double @@ U]
tag: [U](d: Double)@@[Double,U]
scala>
| def fetch[A](id: Int @@ A): A = null.asInstanceOf[A]
fetch: [A](id: @@[Int,A])A
scala>
| def tag[U](arr: Array[Int]):Array[Int @@ U] = arr.asInstanceOf[Array[Int @@ U]]
tag: [U](arr: Array[Int])Array[@@[Int,U]]
scala>
| tag[User](Array(3, 4, 5)).map(_.toString)
res6: Array[String] = Array(3, 4, 5) |
@retronym said: scala>
scala> trait Day; def foo(t: Int with Day) = t == t
<console>:7: error: ambiguous reference to overloaded definition,
both method == in class Object of type (x$1: AnyRef)Boolean
and method == in class Int of type (x: Double)Boolean
match argument types (Int with Day)
trait Day; def foo(t: Int with Day) = t == t
^
error: type mismatch;
found : Double
required: AnyRef
Note: an implicit exists from scala.Double => java.lang.Double, but
methods inherited from Object are rendered ambiguous. This is to avoid
a blanket implicit which would convert any scala.Double to any AnyRef.
You may wish to use a type ascription: `x: java.lang.Double`. |
@JamesIry said: |
Ben Hutchison (ben_hutchison) said: scala> trait Day
defined trait Day
scala> class DayOps(val i: Int with Day) extends AnyVal
<console>:8: error: ambiguous reference to overloaded definition,
both method == in class Object of type (x$1: AnyRef)Boolean
and method == in class Int of type (x: Double)Boolean
match argument types (Int with Day) and expected result type Boolean
class DayOps(val i: Int with Day) extends AnyVal
^
error: type mismatch;
found : Double
required: AnyRef
Note: an implicit exists from scala.Double => java.lang.Double, but
methods inherited from Object are rendered ambiguous. This is to avoid
a blanket implicit which would convert any scala.Double to any AnyRef.
You may wish to use a type ascription: `x: java.lang.Double`. |
@benhutchison said: `~:$> scala scala> trait Day scala> case class Test1(d: Int with Day) scala> trait Day scala> case class Test2(d1: Int with Day, d2: Int with Day) scala> def foo(t: Int with Day) = t == t scala> class DayOps(val i: Int with Day) extends AnyVal |
@retronym said: Would you care to contribute a pull request with test cases for this so we can close this bug? |
@lrytz said: |
Using AnyVal tagged types and case classes leads to hard to understand error messages.
The message is:
The message is:
The text was updated successfully, but these errors were encountered: