(SIP 11) Alternative string interpolation proposal

96 views
Skip to first unread message

martin odersky

unread,
Oct 27, 2011, 9:28:28 AM10/27/11
to scala...@googlegroups.com
I just augmented my string interpolation proposal with an alternative syntax and mechanism. I like the alternative better because it is far more general than the first proposal.

Comments welcome!

 -- Martin


Daniel Sobral

unread,
Oct 27, 2011, 9:54:44 AM10/27/11
to scala...@googlegroups.com
On Thu, Oct 27, 2011 at 11:28, martin odersky <martin....@epfl.ch> wrote:
> I just augmented my string interpolation proposal with an alternative syntax
> and mechanism. I like the alternative better because it is far more general
> than the first proposal.

On the document, it tells me I cannot comment on it. Wasn't the
ability to comment on the proposals one of the goals? Also, I can only
see the document embedded (maybe I'm missing something?), which is
rather uncomfortable.

I do have some comments. I'll wait to see if I should make them here,
or if I should do them on the document.

--
Daniel C. Sobral

I travel to the future all the time.

martin odersky

unread,
Oct 27, 2011, 10:01:46 AM10/27/11
to scala...@googlegroups.com
Hi Daniel, I now opened comments on the Google doc. I am not sure why embedded Google Docs in a SIP mean that the normal comment function on the SIP is disabled, but that seems to be the case. 

Looking forward to your comments.

 -- Martin
--
Martin Odersky
Prof., EPFL and Chairman, Typesafe
PSED, 1015 Lausanne, Switzerland
Tel. EPFL: +41 21 693 6863
Tel. Typesafe: +41 21 691 4967

Daniel Sobral

unread,
Oct 27, 2011, 10:59:39 AM10/27/11
to scala...@googlegroups.com
On Thu, Oct 27, 2011 at 12:01, martin odersky <martin....@epfl.ch> wrote:
> Hi Daniel, I now opened comments on the Google doc. I am not sure why
> embedded Google Docs in a SIP mean that the normal comment function on the
> SIP is disabled, but that seems to be the case.
> Looking forward to your comments.

Thanks. I'm afraid I got carried away with the comments... :-) This
new proposal is vastly superior to the first one.

Rex Kerr

unread,
Oct 27, 2011, 11:06:50 AM10/27/11
to scala...@googlegroups.com
This is great!  I had many reservations about the first version; this one seems splendidly general.

If implicit conversions are in-scope for the string identifier, that would be even better (one could write extensions without having to change StringContext).

And if there were a way to pass formatting information, that would be even better--maybe something with automatic deconstruction of tuples?  I haven't had time to come up with something elegant and general.

  --Rex

Seth Tisue

unread,
Oct 27, 2011, 2:42:11 PM10/27/11
to scala...@googlegroups.com
On Thu, Oct 27, 2011 at 11:06 AM, Rex Kerr <ich...@gmail.com> wrote:
> This is great!  I had many reservations about the first version; this one
> seems splendidly general.

Agree, my reaction exactly. This is marvelous. (And as an old Lisp
hand, I love the macro/quasiquote example.)

{ is of course nicer than \{ nearly all the time, but the JSON use
case gives me pause. See Benjamin Jackman's comment. I wish I had an
idea on this, but I don't.

--
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/

Daniel Sobral

unread,
Oct 27, 2011, 2:54:15 PM10/27/11
to scala...@googlegroups.com
On Thu, Oct 27, 2011 at 13:06, Rex Kerr <ich...@gmail.com> wrote:
> This is great!  I had many reservations about the first version; this one
> seems splendidly general.
>
> If implicit conversions are in-scope for the string identifier, that would
> be even better (one could write extensions without having to change
> StringContext).

Implicits are in scope. See, this works just like for-comprehension
expansion: you rewrite the code as indicated, then process using
standard Scala rules.

That's why it is extensible. You can add methods to StringContext
implicitly. You can add show implicitly. You can add formatted
implicitly.

And note that "id" does *not* need to receive parameters. Pay close
attention to the XML example, and how it works. That's the example
that really brought to me the full impact of the proposal.

> And if there were a way to pass formatting information, that would be even
> better--maybe something with automatic deconstruction of tuples?  I haven't
> had time to come up with something elegant and general.

Err? Beyond { expr ; fmt } expanding to expr.formatted("fmt")? What
else would you need?

>
>   --Rex
>
> On Thu, Oct 27, 2011 at 9:28 AM, martin odersky <martin....@epfl.ch>
> wrote:
>>
>> I just augmented my string interpolation proposal with an alternative
>> syntax and mechanism. I like the alternative better because it is far more
>> general than the first proposal.
>> Comments welcome!
>>  -- Martin
>>
>
>

--

Rex Kerr

unread,
Oct 27, 2011, 3:18:27 PM10/27/11
to scala...@googlegroups.com
On Thu, Oct 27, 2011 at 2:54 PM, Daniel Sobral <dcso...@gmail.com> wrote:

> And if there were a way to pass formatting information, that would be even
> better--maybe something with automatic deconstruction of tuples?  I haven't
> had time to come up with something elegant and general.

Err? Beyond { expr ; fmt } expanding to expr.formatted("fmt")? What
else would you need?


def col(i: Int, n: Int) = ("%"+n+"d").format(i)

def col(i: Int, n: Int) = s"{i;{n}d}"

The latter is okay or not?  If yes, awesome, but that's not exactly what "arbitrary string of characters" brings to mind.  And this is only with the same sort of processing embedded; if you want to switch, it's not clear how one could accomplish that.  If you can't parameterize your formatting, you're missing half the point of parameterizing.

  --Rex

Daniel Sobral

unread,
Oct 27, 2011, 3:29:17 PM10/27/11
to scala...@googlegroups.com

Oh, God, you're right! Sure, { x; 2.2f } looks nicer than { x; "2.2f"
}, but, in practice, you're quite likely to need a formatting computed
at run-time! Please, do comment this on the SIP.

Rex Kerr

unread,
Oct 27, 2011, 3:45:13 PM10/27/11
to scala...@googlegroups.com
This is more a matter for discussion, I think, than a simple comment.  Right now, using { } in XML allows a block (albeit one with newlines disabled):

  <xml>{val a = 7; a*a+a}</xml>

produces <xml>56</xml>.  This is very useful, since it lets you embed complex expressions easily.

But with the current format proposal, this wouldn't work:

  s"{val a = 7; a*a+a}"

would send " a*a+a" as the format string with the empty string as content.  Well, fair enough, use more braces:

  s"{{val a = 7; a*a+a}}"

but, no, {{ and }} are interpreted as escapes, so this gives "{val a = 7; a*a+a}".  So you'd have to

  s"{ {val a = 7; a*a+a} }"

in order to actually get a block, which is sort of weird-looking and possibly fragile, especially if you wanted to combine it with formatting:

  s"{ {val a = 7; a*a+a}; 5d}" // doesn't do what you think 
  s"{ {val a = 7; a*a+a};5d}"  // imbalanced, ugly
  s"{ {val a = 7; a*a+a} ;5d}" // works, but maybe error-prone?

So maybe we can change the rule for escaping braces to be that in normal strings you use \{ and \}, and in raw strings you are out of luck (where out of luck means you use string interpolation: {'{'} and {'}'}).

Then at least you could write blocks with double braces.

But this makes me wonder whether the default shouldn't be a block, with formatting set off by an empty statement:

  s"{val a = 7; a*a+a; ;5d}" // Previous example
  s"{4;;5d}"                 // Just want to print 4 with spaces

Here I'm not sure; this favors complex usage over simple usage, which arguably is the wrong way to go.

  --Rex

Eugene Burmako

unread,
Oct 27, 2011, 4:27:30 PM10/27/11
to scala-sips
Maybe we could use a different symbol as an expr-fmt separator? Say, a
semicolon. In that case, rare occasional semicolons at the end of the
expression can be easily negated by wrapping a relevant part of the
expression in paretheses.
> On Thu, Oct 27, 2011 at 10:01 AM, martin odersky <martin.oder...@epfl.ch>wrote:
>
>
>
> > Hi Daniel, I now opened comments on the Google doc. I am not sure why
> > embedded Google Docs in a SIP mean that the normal comment function on the
> > SIP is disabled, but that seems to be the case.
>
> > Looking forward to your comments.
>
> >  -- Martin
>
> > On Thu, Oct 27, 2011 at 3:54 PM, Daniel Sobral <dcsob...@gmail.com> wrote:
>
> >> On Thu, Oct 27, 2011 at 11:28, martin odersky <martin.oder...@epfl.ch>
> >> wrote:
> >> > I just augmented my string interpolation proposal with an alternative
> >> syntax
> >> > and mechanism. I like the alternative better because it is far more
> >> general
> >> > than the first proposal.
>
> >> On the document, it tells me I cannot comment on it. Wasn't the
> >> ability to comment on the proposals one of the goals? Also, I can only
> >> see the document embedded (maybe I'm missing something?), which is
> >> rather uncomfortable.
>
> >> I do have some comments. I'll wait to see if I should make them here,
> >> or if I should do them on the document.
>
> >> --
> >> Daniel C. Sobral
>
> >> I travel to the future all the time.
>
> > --
> > Martin Odersky
> > Prof., EPFL <http://www.epfl.ch> and Chairman, Typesafe<http://www.typesafe.com>

Eugene Burmako

unread,
Oct 27, 2011, 4:30:04 PM10/27/11
to scala-sips
Oh, sorry, of course, I meant a colon.

Arioch

unread,
Oct 27, 2011, 4:46:12 PM10/27/11
to scala...@googlegroups.com
В письме от Thu, 27 Oct 2011 19:06:50 +0400, Rex Kerr
<ich...@gmail.com> сообщал:

> And if there were a way to pass formatting information, that would be
> even better--maybe something with automatic deconstruction of tuples? I
> haven't had time to come up with something elegant and general.

Maybe sub-expression ? Some standard library ?
Implicit cast to one of formatter classes, which result in string
expression instead of integer?

From s"I ate {n} apples" to like s"I ate {n.format.base(16)} apples"


One more thing is parameters reordering. Natural words order is different
in languages.
Say, in English it is adjective-noun but in Latin/French/Spanish/Italian
it is noun-adjective.
"Living water" or "aqua vita"

If translating any long enough text from English to Russian you found that
either you end with some Frankenstein of language, or you need to reorder
even subclauses in complex sentences.

One more example would be address, should it be ordered from {apartment
No} to {street} to {city} up to {country} ? Or from {country} down to ...
{apartment No} ?

While C sprintf-like specification - template string with placeholders,
followed by array of values - is harder to type and read, it allows
reordering on localisation, without changing program sources.
To integrate GetText-like functionality with StringContext(strings
list).method(valus list) it seems like intermediate layer like
StringContext(strings
list).localizer-and-reoderer(locale-info).method(values list) is required,
but injecting it after JARs are made is not so nice, as to have it in
place already.


--
Написано в почтовом клиенте браузера Opera: http://www.opera.com/mail/

Benjamin Jackman

unread,
Oct 27, 2011, 5:08:10 PM10/27/11
to scala...@googlegroups.com
I think Daniel Sobral's idea would work well. Namely { for " strings and \{ for """ strings. It seems like a pretty decent merge of the two ideas. If some language comes along and makes \{ legal in some part of it's syntax and then becomes popular we'd be in trouble tho.

Benjamin Jackman

unread,
Oct 27, 2011, 5:14:24 PM10/27/11
to scala...@googlegroups.com
I have similar concerns, but have yet to come up with a decent alternative. It feels wrongs to be that {val a = 7; a*a+a} is a different expression depending on the context of the curlies around it. And I don't like the gynamastics required to get it back in line. Maybe something other than ; there, perhaps just a normal ',' I don't think that can come at that location in an expression anyhow? Just brainstorming. 

Arioch

unread,
Oct 27, 2011, 5:19:32 PM10/27/11
to scala...@googlegroups.com
В письме от Fri, 28 Oct 2011 01:14:24 +0400, Benjamin Jackman
<benjamin...@gmail.com> сообщал:

> location in an expression anyhow? Just brainstorming.

If just brainstorming... function(Unit) aka call-by-name

{expression; yet expression; still expression; => now format}

Arioch

unread,
Oct 27, 2011, 5:24:32 PM10/27/11
to scala...@googlegroups.com
В письме от Fri, 28 Oct 2011 01:14:24 +0400, Benjamin Jackman
<benjamin...@gmail.com> сообщал:

> Maybe something other than ; there, perhaps just a normal ','

if to treat it like normal infix method AnyVal => string
{{val a = 7; a*a+a} infix-format 000d }

What are possible symbols not yet taken for one or another operator ? :-)

That would made typing longer, but then format specifiers no more are
special case

Reply all
Reply to author
Forward
0 new messages