Skip to content

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
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions puzzlers/pzzlr-case-defaults.html
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>
Copy link
Collaborator Author

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 prints true, the second one fails to compile", which would be the user wondering whether default args carry over to the apply factory method.

I was thinking of a different code sample that would allow for both options:

case class Part(serialNr: Int = this.hashCode)
println(Part(4322551) == new Part(4322551))
println(Part() == new Part())

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?

<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 (&sect;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 &quot;script object&quot;:
<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>