Note that there are some explanatory texts on larger screens.

plurals
  1. POCast Scala String to StringContext and virtually forward references
    primarykey
    data
    text
    <p>In Scala 2.10.0-RC1 there's a wonderful feature, the <em><a href="http://docs.scala-lang.org/sips/pending/string-interpolation.html" rel="nofollow">String Interpolation</a></em>.</p> <p>This works like this:</p> <pre><code>scala&gt; val x = 1 x: Int = 1 scala&gt; val y = s"Interpolated String $x" y: String = Interpolated String 1 </code></pre> <p>But I'm building a internal DSL with Scala, and therefore I need to handle with a sort of <em>forward references</em>. That you have in mind, what the DSL looks like (it's for documentation generation):</p> <pre><code>object Main { § &gt; "Heading" § &gt;&gt; "Subheading" ++ txt """ Lorem ipsum Abb. ${Main.fig.number} dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. """ val fig = ++ figure( src="https://...", desc="example figure" ) } </code></pre> <p>But the problem looks semantically like this:</p> <pre><code>object X { val y = s"$x with reverse reference" val x = 3 } </code></pre> <p>And this doesn't work, because of the virtually forward reference. I can write <code>x</code> as <code>lazy val</code>, but the compiler moves <code>x</code> and makes it for <code>y</code> available. But for my DSL design the order of <code>y</code> and <code>x</code> <em>must</em> be preserved!</p> <p>So far my solution is a closure, like this:</p> <pre><code>object X { val y = () =&gt; s"$x with reverse reference" val x = 3 } </code></pre> <p>This allows me to invoke <code>X.y()</code> at the end, when <em>all</em> variables are available. But the disadvantage is, the <em>"ugliness"</em>, no domain-user will understand this kind of magic! The domain user must write something like this (but it works!):</p> <pre><code>++ txt (() =&gt; s""" Lorem ipsum Abb. ${Main.fig.number} dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. """) </code></pre> <p>So the idea is to move the closure magic into my DSL api, but therefore I need to pass <code>"$x with reverse reference"</code> as ordinary <code>String</code> and resolve this String at the end as <code>StringContext</code> (s"..."), which resolves the <code>$</code>-references. But so far I found nothing, how to cast this. In short how can I cast <code>"foo $x bar</code> to <code>s"foo $x bar"</code>?</p> <p>What I <a href="http://dcsobral.blogspot.co.uk/2012/01/string-interpolation-on-scala-210.html" rel="nofollow">found</a> is, how to individualize <code>StringContext</code>, but don't know how I can use this for my problem:</p> <pre><code>scala&gt; case class StringContext(parts: String*) { | def rr (args: Any*) = scala.StringContext(parts: _*).s(args: _*) | } defined class StringContext scala&gt; rr"hi" res3: String = hi scala&gt; rr"hi $s" res4: String = hi 1 </code></pre> <p>The other idea is a less "noisy" lambda notation, but how can I write something like (or similar) this:</p> <pre><code>++ txt =&gt; s"""... ${Main.fig.number} ...""" </code></pre> <p>Such a notation would be reasonable. I don't necessarily to write my own String parser with reference resolving. Anybody an sneaky idea?</p> <p>The DSL api is semantically built like this:</p> <pre><code>object ++ { def txt (s: String) = ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload