AlanP
unread,May 31, 2011, 5:30:18 PM5/31/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to scala-user
I'm getting a behavior with implicits that does not seem consistent to
me. It is easier if I show it with code.
The following code in Exhibit 1(below) gets no compiler error, but
does trigger a NullPointerException at runtime. It can be fixed making
'val a' lazy, in object I. So it seems like an order of initialization
issue, except that it is between different objects that refer to each
other.
Then the code in Exhibit 2 is the same as Exhibit 1, except that
object A is being held directly in 'val a', instead of being inside a
List. In this case I get no runtime error, even without making 'a'
lazy, it runs as intended.
It seems to me that: 1) I should get the same behavior with Lists or
direct objects or, 2) I should be able to get a compile-time error on
Exhibit 1.
If this is the expected behavior, can someone explain me what's the
reasoning involved?
Thanks,
Alan
------ Exhibit 1 -------
trait C {val a: List[D]}
class D(implicit val c: C)
object ImplicitHolder {
implicit object I extends C {
val a = List(A) // can fix runtime error by making it lazy
}
}
import ImplicitHolder._
object A extends D {
val b = c.a.head
val s = "hello"
}
object Circularity extends App {
println(I.a.head.s)
}
--------------------------------
------ Exhibit 2 -----------
trait C {val a: D}
class D(implicit val c: C)
object ImplicitHolder {
implicit object I extends C {
val a = A
}
}
import ImplicitHolder._
object A extends D {
val b = c.a
val s = "hello"
}
object Circularity extends App {
println(I.a.s)
}
--------------------------