-
Notifications
You must be signed in to change notification settings - Fork 54
Initial draft of puzzler about default args for case classes #116
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<h1>Well, In This Case...</h1> | ||
<table class="table meta-table table-condensed"> | ||
<tbody> | ||
<tr> | ||
<td class="header-column"><strong>Contributed by</strong></td> | ||
<td>Andrew Phillips</td> | ||
</tr> | ||
<tr> | ||
<td><strong>Source</strong></td> | ||
<td><a target="_blank" href="https://groups.google.com/d/topic/scala-language/DDpvGcrs3QQ/">Didier Dupont </a></td> | ||
</tr> | ||
<tr> | ||
<td><strong>First tested with Scala version</strong></td> | ||
<td>2.11.2</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<div class="code-snippet"> | ||
<h3>What is the result of executing the following code?</h3> | ||
<pre class="prettyprint lang-scala"> | ||
case class Toy(squeezeMsg: String = this.toString) { | ||
override def toString = squeezeMsg | ||
} | ||
println(Toy("My name is Fido!") == new Toy("My name is Fido!")) | ||
println(Toy() == new Toy()) | ||
</pre> | ||
<ol> | ||
<li>Fails to compile</li> | ||
<li id="correct-answer">Prints: | ||
<pre class="prettyprint lang-scala"> | ||
true | ||
false | ||
</pre> | ||
</li> | ||
<li>Prints: | ||
<pre class="prettyprint lang-scala"> | ||
true | ||
true | ||
</pre> | ||
</li> | ||
<li>The first statement prints: | ||
<pre class="prettyprint lang-scala"> | ||
true | ||
</pre> | ||
and the second throws a runtime exception | ||
</li> | ||
</ol> | ||
</div> | ||
<button id="show-and-tell" class="btn btn-primary" href="#">Display the correct answer, explanation and comments</button> | ||
<div id="explanation" class="explanation" style="display:none"> | ||
<h3>Explanation</h3> | ||
<p> | ||
As per the SLS (§5.3.2), if the elements (i.e. constructor parameters) of | ||
a case class have default arguments, these are carried over to the | ||
<tt>apply</tt> factory method on the companion object that is generated by | ||
the compiler. Also, when <tt>Toy</tt> is declared, there is indeed a | ||
<tt>this</tt> reference in scope, so the code compiles successfully. | ||
</p> | ||
<p> | ||
The <tt>this</tt> in <tt>this.toString</tt> (the value of the default argument | ||
for <tt>squeezeMsg</tt>) does not refer to the instance of <tt>Toy</tt> being | ||
created, however. Instead, it resolves to the instance of the class or object | ||
in which <tt>Toy</tt>'s constructor is defined. | ||
</p> | ||
<p> | ||
When running the example in the REPL, the class or object invoking the | ||
constructor <tt>new Toy()</tt> directly is a generated "script object": | ||
<pre class="prettyprint lang-scala"> | ||
scala> println(new Toy()) | ||
$line46.$read$$iw$$iw$@7335aa7c | ||
</pre> | ||
When creating a new <tt>Toy</tt> instance using the standard factory method | ||
generated for case classes, however, the class or object invoking <tt>Toy</tt>'s | ||
constructor is actually the companion object <tt>Toy</tt>: | ||
<pre class="prettyprint lang-scala"> | ||
scala> println(Toy()) | ||
Toy | ||
</pre> | ||
</p> | ||
<p> | ||
Since the generated implementation of <tt>equals</tt> for case classes | ||
uses the values of the case class's elements (<tt>squeezeMsg</tt>, in this case) | ||
to determine equality, the two instances <tt>Toy()</tt> and <tt>new Toy()</tt> | ||
are, thus, <em>not</em> equal. | ||
</p> | ||
</div> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So many choices here! I found it really hard to decide between this one (which is probably the reader thinking "how can it compile with a dangling
this
?) and "The first statement printstrue
, the second one fails to compile", which would be the user wondering whether default args carry over to theapply
factory method.I was thinking of a different code sample that would allow for both options:
Here, the candidate answer with the runtime exception (i.e. endless loop) can be dropped and we can have both possible "fails to compile" answers. But somehow this variant didn't seem quite as "puzzling."
@didierd: What do you think?