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
Added a puzzler on extracting nulls as values in Maps #55
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<h1>null will be the death of me</h1> | ||
<table class="table meta-table table-condensed"> | ||
<tbody> | ||
<tr> | ||
<td class="header-column"><strong>Contributed by</strong></td> | ||
<td>Vassil Dichev</td> | ||
</tr> | ||
<tr> | ||
<td><strong>Source</strong></td> | ||
<!-- | ||
If no source (your puzzler is not mentioned anywhere on the web), just say N/A | ||
--> | ||
<td><a target="_blank" href="N/A">N/A</a></td> | ||
</tr> | ||
<tr> | ||
<td><strong>Tested with Scala version</strong></td> | ||
<td>2.9.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"> | ||
val map = Map("death" -> null) | ||
val result = map match { | ||
case m: Map[String,Int] => m.get("death") flatMap Option.apply | ||
} | ||
println(result) | ||
</pre> | ||
<ol> | ||
<!-- | ||
The correct answer should have id for corresponding li element equal to "correct-answer". | ||
When placing code snippets wrap them with pre tag: | ||
<pre class="prettyprint lang-scala"> | ||
... | ||
</pre> | ||
--> | ||
<li>throws <code>ClassCastException</code></li> | ||
<li>throws <code>NullPointerException</code></li> | ||
<li><code>None</code></li> | ||
<li id="correct-answer"><code>Some(0)</code></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> | ||
This is yet another example of why you should not ignore the compiler's warnings. This code is typical of deserialization from e.g. JSON. The snippet doesn't throw a <code>ClassCastException</code>, because due to erasure the object is only checked if it's a <code>Map</code>. | ||
</p> | ||
<p> | ||
What is interesting is that this works as expected: | ||
<pre class="prettyprint lang-scala"> | ||
map match { | ||
case m: Map[String,Int] => Option(m("death")) | ||
} | ||
</pre> | ||
if what you expected was <code>None</code>. | ||
</p> | ||
<p> | ||
So what is the explanation of this? Scala creates a function object as an argument to <code>flatMap</code> and it needs to unbox and box the parameter from <code>Integer</code> to <code>int</code> and back again. However, on the JVM unboxing <code>null</code> to an integral type(such as <code>byte</code>, <code>short</code>, <code>int</code> or <code>long</code>), results in 0, and Scala is also affected by this behaviour. | ||
</p> | ||
<p> | ||
The moral of the story is that erasure warnings are insidious since they will sometimes result in unexpected behaviour in a different location than the one which triggers the warning. Another thing we learn is that nulls do not fit well with autoboxing in Java, and Scala must bear this burden of integrating with the JVM. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another incorrect comment: Scala actually differs from how unboxing is implemented in Java. |
||
</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.
Actually, Java implements unboxing of null by throwing a NullPointerException. I'm not sure which is more surprising.