Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn on selection of vals from DelayedInit subclasses.
Which are likely to yield null, if the program didn't start. This is a common source of confusion for people new to the language, as was seen during the Coursera course. The test case shows that the usage pattern within Specs2 won't generate these warnings.
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
delayed-init-ref.scala:17: error: Selecting value vall from object O, which extends scala.DelayedInit, is likely to yield an uninitialized value | |||
println(O.vall) // warn | |||
^ | |||
delayed-init-ref.scala:19: error: Selecting value vall from object O, which extends scala.DelayedInit, is likely to yield an uninitialized value | |||
println(vall) // warn | |||
^ | |||
delayed-init-ref.scala:40: error: Selecting value foo from trait UserContext, which extends scala.DelayedInit, is likely to yield an uninitialized value | |||
println({locally(()); this}.foo) // warn (spurious, but we can't discriminate) | |||
^ | |||
three errors found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
-Xlint -Xfatal-warnings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
trait T { | |||
val traitVal = "" | |||
} | |||
|
|||
object O extends App with T { | |||
val vall = "" | |||
lazy val lazyy = "" | |||
def deff = "" | |||
|
|||
println(vall) // no warn | |||
new { | |||
println(vall) // no warn | |||
} | |||
} | |||
|
|||
object Client { | |||
println(O.vall) // warn | |||
import O.vall | |||
println(vall) // warn | |||
|
|||
println(O.lazyy) // no warn | |||
println(O.deff) // no warn | |||
println(O.traitVal) // no warn | |||
} | |||
|
|||
// Delayed init usage pattern from Specs2 | |||
// See: https://groups.google.com/d/msg/scala-sips/wP6dL8nIAQs/ogjoPE-MSVAJ | |||
trait Before extends DelayedInit { | |||
def before() | |||
override def delayedInit(x: => Unit): Unit = { before; x } | |||
} | |||
object Spec { | |||
trait UserContext extends Before { | |||
def before() = () | |||
val foo = "foo" | |||
} | |||
new UserContext { | |||
println(foo) // no warn | |||
println(this.foo) // no warn | |||
println({locally(()); this}.foo) // warn (spurious, but we can't discriminate) | |||
} | |||
} |