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

Conversation

demobox
Copy link
Collaborator

@demobox demobox commented Nov 30, 2014

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?

@demobox
Copy link
Collaborator Author

demobox commented Jan 23, 2015

Thanks for the comments and references, @som-snytt!

Suggested by review comments by @som-snytt. Thanks!
@demobox
Copy link
Collaborator Author

demobox commented Feb 25, 2015

Merged to master: 7176928

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants