You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Trond Olsen (tolsen77) said (edited on Aug 17, 2011 11:20:16 PM UTC):
Here is a simplified example of the looping implicit:
{noformat}class A
class B(val name: String) extends A
implicit def aToB(a: A): B = { println("aToB"); a }
println((new A).name)
// leads to infinite loop on implicit aToB()
{noformat}
Is this a programmer error, or should the typechecker make sure an implicit shouldn't be applicable within its own definition?
On second thought, it should fail completely. It's entirely nonsensical since a should never become a B, except the implicit is of a different opinion, hehe.
See attachments planets3.scala and planets5.scala
The only difference is that planets3.scala has runtime casting:
implicit def valueToPlanetVal(x: Value) = x.asInstanceOf[Val]
and planets5.scala an explicit return type:
implicit def valueToPlanetVal(x: Value) : Val = x
Both compile without errors.
planets3.scala is running well.
planets5.scala hangs and never exutes the last line and without an error message:
println("Mars mass: " + Planet.withName("Mars").mass + " kg")
The text was updated successfully, but these errors were encountered: