Skip to content
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

Interpolation bitter desugaring #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions puzzlers/pzzlr-072.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<h1>Strung Along</h1>
<table class="table meta-table table-condensed">
<tbody>
<tr>
<td class="header-column"><strong>Contributed by</strong></td>
<td>A. P. Marki</td>
</tr>
<tr>
<td><strong>Source</strong></td>
<td><a target="_blank" href="https://gitter.im/scala/contributors?at=5b249065202c8f71f506067b">Gitter</a></td>
</tr>
<tr>
<td><strong>First tested with Scala version</strong></td>
<td>2.12.6</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 StringContext: String = s"hello, world"
</pre>
<ol>
<li>It fails to compile with a type mismatch. It found a StringContext but requires a String.</li>
<li id="correct-answer">It fails to compile with a type mismatch. It found a String but requires an Int.</li>
<li>It compiles but at runtime it throws StringIndexOutOfBoundsException.</li>
<li>It fails to compile because method s is not a member of String.</li>
<li>It fails to compile by throwing scala.reflect.internal.MissingRequirementError: object scala.StringContext in compiler mirror not found.</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>
The interpolation desugars unsurprisingly to:
<pre class="prettyprint lang-scala">
StringContext("hello, world").s()
</pre>
</p>
<p>
<tt>StringContext</tt>, in this context, does not mean <tt>scala.StringContext</tt> but instead means
the current recursive definition, which is of type <tt>String</tt>.
</p>
<p>
<tt>String.apply</tt> takes an <tt>Int</tt> and performs lookup at that index.
</p>
<p>
The <tt>s</tt> comes first in the source but second in the expansion, so there is no error to indicate
that there is no "s-interpolator" available. That error could be made visible, in a way:
<pre class="prettyprint lang-scala">
scala> implicit def f(s: String): Int = s.length
f: (s: String)Int

scala> val StringContext: String = s"hello, world"
<console>:12: error: value s is not a member of Char
val StringContext: String = s"hello, world"
^
</pre>
</p>
</div>