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
NullPointerException when re-defining a val set in the context of itself #9241
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9241?orig=1 |
@som-snytt said: The book does not propose it as a helpful example at the citation. Footnotes are at the end of the section. The real problem lies in the forward-referenced section. Is no one bothered by the semi-colons? Are we barbarians? Civilized people use Also, the explanation of scoping in the REPL, albeit merely a "conceptual visualization", i.e., a metaphor, is a bit imprecise. I would expect the stack trace to be truncated:
|
Jordan Pilat (jrpilat) said (edited on Mar 22, 2015 5:25:40 AM UTC): |
Jordan Pilat (jrpilat) said: |
@som-snytt said: The variable on the RHS refers to the variable on the LHS being defined, hence it is recursive. You can google it. I am far too lazy for such an undertaking. The REPL will not actually bring the first variable into scope at all, which is slightly different from what the book says; such a distinction may only matter with certain imports. Actually, that's not true. For some reason, it pulls in the import. But the mechanism is known to be imprecise. As a rule, it tries to import only what it needs from history. scala> val mySet: Set[String] = mySet + "Item3"
[[syntax trees at end of typer]] // <console>
package $line2 {
object $read extends scala.AnyRef {
def <init>(): $line2.$read.type = {
$read.super.<init>();
()
};
object $iw extends scala.AnyRef {
def <init>(): type = {
$iw.super.<init>();
()
};
import $line1.$read.$iw.$iw.mySet;
object $iw extends scala.AnyRef {
def <init>(): type = {
$iw.super.<init>();
()
};
private[this] val mySet: Set[String] = $iw.this.mySet.+("Item3");
<stable> <accessor> def mySet: Set[String] = $iw.this.mySet
}
}
}
} |
Background:
Odersky's book, "+Programming in Scala, Second Edition+", Chapter 2, Footnotes, reads:
[5] In the interpreter, however, you can define a new val with a name that was already used before....
Test Case:
Expected Output:
Actual Output:
Additional notes:
Running the below code:
...results in the following output:
It is only when we try to redefine mySet as this output, as described in the book, that we encounter issues.
The text was updated successfully, but these errors were encountered: