This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: Expression placeholder for Strings??

24 replies
Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.

Thats pretty difficult to get from "I'm new in Scala". Its quite
possible you've come from a perl background or any other number of
scripting languages.

The string format family of functions gives similar ability with alot
more power (i.e. the formatting). Other than that what you seem to be
asking for is an eval type language, of which scala definitely isn't.

Having said that it would be fairly trivial to scan strings in a
compiler plugin and extract from this syntax. I personally can't see
the point. The + syntax and the format string functions cover the
majority of use cases. I'm not sure what the ${syntax} buys you.

On Tue, Mar 17, 2009 at 11:34 AM, Martin Kuhn wrote:
> Of course I'm aware that it is possible to build a string in this way.
>
> Anyway, I'm wondering why such a powerful language has nothing in this context
>
>
> On Tue, Mar 17, 2009 at 11:19 AM, Chris Twiner wrote:
>> Is
>>
>> println("My Val " +myVal)
>>
>> ok? "Most" objects in scala will have sensible defaults for toString
>> which is being called in the above example. You can override this
>> function for your own classes (and via mixins some existing classes).
>>
>> On Tue, Mar 17, 2009 at 11:12 AM, Martin Kuhn wrote:
>>>
>>> Hi,
>>>
>>> I'm new in Scala and I'm wondering if there it is not possible to use a
>>> placeholder for expressions in strings (like in scala xml)
>>> I found nothing in this context...
>>>
>>> e.g.
>>> val myVal="That's it"
>>> println("My Val ${myVal}")
>>>
>>> Could anybody give me a hint?
>>>
>>> TIA
>>> Martin
>>>
>>>
>>> --
>>> View this message in context: http://www.nabble.com/Expression-placeholder-for-Strings---tp22556004p22...
>>> Sent from the Scala - User mailing list archive at Nabble.com.
>>>
>>>
>>
>>
>

DRMacIver
Joined: 2008-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

2009/3/17 Chris Twiner :
> Thats pretty difficult to get from "I'm new in Scala".  Its quite
> possible you've come from a perl background or any other number of
> scripting languages.
>
> The string format family of functions gives similar ability with alot
> more power (i.e. the formatting).  Other than that what you seem to be
> asking for is an eval type language, of which scala definitely isn't.
>
> Having said that it would be fairly trivial to scan strings in a
> compiler plugin and extract from this syntax.  I personally can't see
> the point.  The + syntax and the format string functions cover the
> majority of use cases.  I'm not sure what the ${syntax} buys you.

"""
this is a lot of text
more text here
I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
It works fine if """+text.isSmall+""" but quickly gets ugly
"""

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Expression placeholder for Strings??

"I'm not sure what the ${syntax} buys you"

Readability. Not leaving your String literal just to include a
variable's contents. Not having potential confusion around spaces.
Readability.

println("Hello, ${name}. How are you today?")

paulp, take it away.

2009/3/17 David MacIver :
> 2009/3/17 Chris Twiner :
>> Thats pretty difficult to get from "I'm new in Scala".  Its quite
>> possible you've come from a perl background or any other number of
>> scripting languages.
>>
>> The string format family of functions gives similar ability with alot
>> more power (i.e. the formatting).  Other than that what you seem to be
>> asking for is an eval type language, of which scala definitely isn't.
>>
>> Having said that it would be fairly trivial to scan strings in a
>> compiler plugin and extract from this syntax.  I personally can't see
>> the point.  The + syntax and the format string functions cover the
>> majority of use cases.  I'm not sure what the ${syntax} buys you.
>
> """
> this is a lot of text
> more text here
> I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
> It works fine if """+text.isSmall+""" but quickly gets ugly
> """
>

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Expression placeholder for Strings??


On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
"I'm not sure what the ${syntax} buys you"

Readability.  Not leaving your String literal just to include a
variable's contents.  Not having potential confusion around spaces.
Readability.

println("Hello, ${name}.  How are you today?")

paulp, take it away.

Then the IDE and the compiler has to make sure that when you rename a field, it updates all strings refering to it, as well as warning for spelling errors or unused literals.
 


2009/3/17 David MacIver <david.maciver@gmail.com>:
> 2009/3/17 Chris Twiner <chris.twiner@gmail.com>:
>> Thats pretty difficult to get from "I'm new in Scala".  Its quite
>> possible you've come from a perl background or any other number of
>> scripting languages.
>>
>> The string format family of functions gives similar ability with alot
>> more power (i.e. the formatting).  Other than that what you seem to be
>> asking for is an eval type language, of which scala definitely isn't.
>>
>> Having said that it would be fairly trivial to scan strings in a
>> compiler plugin and extract from this syntax.  I personally can't see
>> the point.  The + syntax and the format string functions cover the
>> majority of use cases.  I'm not sure what the ${syntax} buys you.
>
> """
> this is a lot of text
> more text here
> I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
> It works fine if """+text.isSmall+""" but quickly gets ugly
> """
>



--
Viktor Klang
Senior Systems Analyst
Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

Ok, I'll bite -> expanded text -> I don't see what it buys you when
you have no eval syntax possible in scala. I was fairly sure the rest
of the text in the email made that clear, but apparently not.

On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson
wrote:
> "I'm not sure what the ${syntax} buys you"
>
> Readability.  Not leaving your String literal just to include a
> variable's contents.  Not having potential confusion around spaces.
> Readability.
>
> println("Hello, ${name}.  How are you today?")
>
> paulp, take it away.
>
> 2009/3/17 David MacIver :
>> 2009/3/17 Chris Twiner :
>>> Thats pretty difficult to get from "I'm new in Scala".  Its quite
>>> possible you've come from a perl background or any other number of
>>> scripting languages.
>>>
>>> The string format family of functions gives similar ability with alot
>>> more power (i.e. the formatting).  Other than that what you seem to be
>>> asking for is an eval type language, of which scala definitely isn't.
>>>
>>> Having said that it would be fairly trivial to scan strings in a
>>> compiler plugin and extract from this syntax.  I personally can't see
>>> the point.  The + syntax and the format string functions cover the
>>> majority of use cases.  I'm not sure what the ${syntax} buys you.
>>
>> """
>> this is a lot of text
>> more text here
>> I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
>> It works fine if """+text.isSmall+""" but quickly gets ugly
>> """
>>
>

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Expression placeholder for Strings??

The compiler doesn't have to, no. And the IDE can see it as sugar,
like it should for for-comprehensions.

2009/3/17 Viktor Klang :
>
>
> On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson
> wrote:
>>
>> "I'm not sure what the ${syntax} buys you"
>>
>> Readability.  Not leaving your String literal just to include a
>> variable's contents.  Not having potential confusion around spaces.
>> Readability.
>>
>> println("Hello, ${name}.  How are you today?")
>>
>> paulp, take it away.
>
> Then the IDE and the compiler has to make sure that when you rename a field,
> it updates all strings refering to it, as well as warning for spelling
> errors or unused literals.
>
>>
>> 2009/3/17 David MacIver :
>> > 2009/3/17 Chris Twiner :
>> >> Thats pretty difficult to get from "I'm new in Scala".  Its quite
>> >> possible you've come from a perl background or any other number of
>> >> scripting languages.
>> >>
>> >> The string format family of functions gives similar ability with alot
>> >> more power (i.e. the formatting).  Other than that what you seem to be
>> >> asking for is an eval type language, of which scala definitely isn't.
>> >>
>> >> Having said that it would be fairly trivial to scan strings in a
>> >> compiler plugin and extract from this syntax.  I personally can't see
>> >> the point.  The + syntax and the format string functions cover the
>> >> majority of use cases.  I'm not sure what the ${syntax} buys you.
>> >
>> > """
>> > this is a lot of text
>> > more text here
>> > I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
>> > It works fine if """+text.isSmall+""" but quickly gets ugly
>> > """
>> >
>
>
>
> --
> Viktor Klang
> Senior Systems Analyst
>

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Expression placeholder for Strings??

I'm really not thinking of this as being eval.

"Hello ${name}. How are you today?" should expand, at compile time, to:

() => "Hello " + name + ". How are you today?"

and when referred to, it should be applied. If name is a val or lazy
val, it would be ok to make it a String, not a lambda.

2009/3/17 Chris Twiner :
> Ok, I'll bite -> expanded text -> I don't see what it buys you when
> you have no eval syntax possible in scala.  I was fairly sure the rest
> of the text in the email made that clear, but apparently not.
>
> On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson
> wrote:
>> "I'm not sure what the ${syntax} buys you"
>>
>> Readability.  Not leaving your String literal just to include a
>> variable's contents.  Not having potential confusion around spaces.
>> Readability.
>>
>> println("Hello, ${name}.  How are you today?")
>>
>> paulp, take it away.
>>
>> 2009/3/17 David MacIver :
>>> 2009/3/17 Chris Twiner :
>>>> Thats pretty difficult to get from "I'm new in Scala".  Its quite
>>>> possible you've come from a perl background or any other number of
>>>> scripting languages.
>>>>
>>>> The string format family of functions gives similar ability with alot
>>>> more power (i.e. the formatting).  Other than that what you seem to be
>>>> asking for is an eval type language, of which scala definitely isn't.
>>>>
>>>> Having said that it would be fairly trivial to scan strings in a
>>>> compiler plugin and extract from this syntax.  I personally can't see
>>>> the point.  The + syntax and the format string functions cover the
>>>> majority of use cases.  I'm not sure what the ${syntax} buys you.
>>>
>>> """
>>> this is a lot of text
>>> more text here
>>> I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
>>> It works fine if """+text.isSmall+""" but quickly gets ugly
>>> """
>>>
>>
>

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Expression placeholder for Strings??

I haven't been active in any language community whose language already
has this feature. So, I'll ask. Does it cause problems for Groovy,
Perl or PHP programmers? I realise Perl at least can eval at runtime
if necessary, but I don't think it does by default. I don't think
Groovy has that luxury, but I don't know it enough to be sure.

2009/3/17 Chris Twiner :
> So the rule would also be, this syntax cannot be used in string
> parameters to functions, its not actually a real string.  At the very
> least it shouldn't look like a real string then, different dividers
> than " othewise you'll only be answering emails from other users as to
> why this string isn't actually a string after desugaring.
>
> On Tue, Mar 17, 2009 at 12:18 PM, Ricky Clarkson
> wrote:
>> I'm really not thinking of this as being eval.
>>
>> "Hello ${name}.  How are you today?" should expand, at compile time, to:
>>
>> () => "Hello " + name + ".  How are you today?"
>>
>> and when referred to, it should be applied.  If name is a val or lazy
>> val, it would be ok to make it a String, not a lambda.
>>
>> 2009/3/17 Chris Twiner :
>>> Ok, I'll bite -> expanded text -> I don't see what it buys you when
>>> you have no eval syntax possible in scala.  I was fairly sure the rest
>>> of the text in the email made that clear, but apparently not.
>>>
>>> On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson
>>> wrote:
>>>> "I'm not sure what the ${syntax} buys you"
>>>>
>>>> Readability.  Not leaving your String literal just to include a
>>>> variable's contents.  Not having potential confusion around spaces.
>>>> Readability.
>>>>
>>>> println("Hello, ${name}.  How are you today?")
>>>>
>>>> paulp, take it away.
>>>>
>>>> 2009/3/17 David MacIver :
>>>>> 2009/3/17 Chris Twiner :
>>>>>> Thats pretty difficult to get from "I'm new in Scala".  Its quite
>>>>>> possible you've come from a perl background or any other number of
>>>>>> scripting languages.
>>>>>>
>>>>>> The string format family of functions gives similar ability with alot
>>>>>> more power (i.e. the formatting).  Other than that what you seem to be
>>>>>> asking for is an eval type language, of which scala definitely isn't.
>>>>>>
>>>>>> Having said that it would be fairly trivial to scan strings in a
>>>>>> compiler plugin and extract from this syntax.  I personally can't see
>>>>>> the point.  The + syntax and the format string functions cover the
>>>>>> majority of use cases.  I'm not sure what the ${syntax} buys you.
>>>>>
>>>>> """
>>>>> this is a lot of text
>>>>> more text here
>>>>> I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
>>>>> It works fine if """+text.isSmall+""" but quickly gets ugly
>>>>> """
>>>>>
>>>>
>>>
>>
>

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Expression placeholder for Strings??

Just please, do *not* call them g-strings, no matter how groovy it is.

Sent from my iPhone

On Mar 17, 2009, at 7:09 AM, Ricky Clarkson
wrote:

> The compiler doesn't have to, no. And the IDE can see it as sugar,
> like it should for for-comprehensions.
>
> 2009/3/17 Viktor Klang :
>>
>>
>> On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson > >
>> wrote:
>>>
>>> "I'm not sure what the ${syntax} buys you"
>>>
>>> Readability. Not leaving your String literal just to include a
>>> variable's contents. Not having potential confusion around spaces.
>>> Readability.
>>>
>>> println("Hello, ${name}. How are you today?")
>>>
>>> paulp, take it away.
>>
>> Then the IDE and the compiler has to make sure that when you rename
>> a field,
>> it updates all strings refering to it, as well as warning for
>> spelling
>> errors or unused literals.
>>
>>>
>>> 2009/3/17 David MacIver :
>>>> 2009/3/17 Chris Twiner :
>>>>> Thats pretty difficult to get from "I'm new in Scala". Its quite
>>>>> possible you've come from a perl background or any other number of
>>>>> scripting languages.
>>>>>
>>>>> The string format family of functions gives similar ability with
>>>>> alot
>>>>> more power (i.e. the formatting). Other than that what you seem
>>>>> to be
>>>>> asking for is an eval type language, of which scala definitely
>>>>> isn't.
>>>>>
>>>>> Having said that it would be fairly trivial to scan strings in a
>>>>> compiler plugin and extract from this syntax. I personally
>>>>> can't see
>>>>> the point. The + syntax and the format string functions cover the
>>>>> majority of use cases. I'm not sure what the ${syntax} buys you.
>>>>
>>>> """
>>>> this is a lot of text
>>>> more text here
>>>> I don't think this is is very elegant, do you """+chrisTwiner.name
>>>> +"""?
>>>> It works fine if """+text.isSmall+""" but quickly gets ugly
>>>> """
>>>>
>>
>>
>>
>> --
>> Viktor Klang
>> Senior Systems Analyst
>>

Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

So the rule would also be, this syntax cannot be used in string
parameters to functions, its not actually a real string. At the very
least it shouldn't look like a real string then, different dividers
than " othewise you'll only be answering emails from other users as to
why this string isn't actually a string after desugaring.

On Tue, Mar 17, 2009 at 12:18 PM, Ricky Clarkson
wrote:
> I'm really not thinking of this as being eval.
>
> "Hello ${name}.  How are you today?" should expand, at compile time, to:
>
> () => "Hello " + name + ".  How are you today?"
>
> and when referred to, it should be applied.  If name is a val or lazy
> val, it would be ok to make it a String, not a lambda.
>
> 2009/3/17 Chris Twiner :
>> Ok, I'll bite -> expanded text -> I don't see what it buys you when
>> you have no eval syntax possible in scala.  I was fairly sure the rest
>> of the text in the email made that clear, but apparently not.
>>
>> On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson
>> wrote:
>>> "I'm not sure what the ${syntax} buys you"
>>>
>>> Readability.  Not leaving your String literal just to include a
>>> variable's contents.  Not having potential confusion around spaces.
>>> Readability.
>>>
>>> println("Hello, ${name}.  How are you today?")
>>>
>>> paulp, take it away.
>>>
>>> 2009/3/17 David MacIver :
>>>> 2009/3/17 Chris Twiner :
>>>>> Thats pretty difficult to get from "I'm new in Scala".  Its quite
>>>>> possible you've come from a perl background or any other number of
>>>>> scripting languages.
>>>>>
>>>>> The string format family of functions gives similar ability with alot
>>>>> more power (i.e. the formatting).  Other than that what you seem to be
>>>>> asking for is an eval type language, of which scala definitely isn't.
>>>>>
>>>>> Having said that it would be fairly trivial to scan strings in a
>>>>> compiler plugin and extract from this syntax.  I personally can't see
>>>>> the point.  The + syntax and the format string functions cover the
>>>>> majority of use cases.  I'm not sure what the ${syntax} buys you.
>>>>
>>>> """
>>>> this is a lot of text
>>>> more text here
>>>> I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
>>>> It works fine if """+text.isSmall+""" but quickly gets ugly
>>>> """
>>>>
>>>
>>
>

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Expression placeholder for Strings??

(assuming you meant to reply to the list, so replying to the list myself)

I agree with you.

2009/3/17 Martin Kuhn :
> I didn't thought that my simple question leads to such a heavy response.
>
> I think most "dynamic" languages out there have such a feature.
>
> e.g. Groovy
> date = new Date(0)
> out = "Year $date.year Month $date.month Day $date.date"
> assert out == 'Year 70 Month 0 Day 1'
> out = "Date is ${date.toGMTString()} !"
>
>
> From my point of view it leads to cleaner source code when you have
> such a feature (especially when you have to build complex strings with
> variables)
>
>
>
>
>
> On Tue, Mar 17, 2009 at 12:24 PM, Ricky Clarkson
> wrote:
>> I haven't been active in any language community whose language already
>> has this feature.  So, I'll ask.  Does it cause problems for Groovy,
>> Perl or PHP programmers?  I realise Perl at least can eval at runtime
>> if necessary, but I don't think it does by default.  I don't think
>> Groovy has that luxury, but I don't know it enough to be sure.
>>
>> 2009/3/17 Chris Twiner :
>>> So the rule would also be, this syntax cannot be used in string
>>> parameters to functions, its not actually a real string.  At the very
>>> least it shouldn't look like a real string then, different dividers
>>> than " othewise you'll only be answering emails from other users as to
>>> why this string isn't actually a string after desugaring.
>>>
>>> On Tue, Mar 17, 2009 at 12:18 PM, Ricky Clarkson
>>> wrote:
>>>> I'm really not thinking of this as being eval.
>>>>
>>>> "Hello ${name}.  How are you today?" should expand, at compile time, to:
>>>>
>>>> () => "Hello " + name + ".  How are you today?"
>>>>
>>>> and when referred to, it should be applied.  If name is a val or lazy
>>>> val, it would be ok to make it a String, not a lambda.
>>>>
>>>> 2009/3/17 Chris Twiner :
>>>>> Ok, I'll bite -> expanded text -> I don't see what it buys you when
>>>>> you have no eval syntax possible in scala.  I was fairly sure the rest
>>>>> of the text in the email made that clear, but apparently not.
>>>>>
>>>>> On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson
>>>>> wrote:
>>>>>> "I'm not sure what the ${syntax} buys you"
>>>>>>
>>>>>> Readability.  Not leaving your String literal just to include a
>>>>>> variable's contents.  Not having potential confusion around spaces.
>>>>>> Readability.
>>>>>>
>>>>>> println("Hello, ${name}.  How are you today?")
>>>>>>
>>>>>> paulp, take it away.
>>>>>>
>>>>>> 2009/3/17 David MacIver :
>>>>>>> 2009/3/17 Chris Twiner :
>>>>>>>> Thats pretty difficult to get from "I'm new in Scala".  Its quite
>>>>>>>> possible you've come from a perl background or any other number of
>>>>>>>> scripting languages.
>>>>>>>>
>>>>>>>> The string format family of functions gives similar ability with alot
>>>>>>>> more power (i.e. the formatting).  Other than that what you seem to be
>>>>>>>> asking for is an eval type language, of which scala definitely isn't.
>>>>>>>>
>>>>>>>> Having said that it would be fairly trivial to scan strings in a
>>>>>>>> compiler plugin and extract from this syntax.  I personally can't see
>>>>>>>> the point.  The + syntax and the format string functions cover the
>>>>>>>> majority of use cases.  I'm not sure what the ${syntax} buys you.
>>>>>>>
>>>>>>> """
>>>>>>> this is a lot of text
>>>>>>> more text here
>>>>>>> I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
>>>>>>> It works fine if """+text.isSmall+""" but quickly gets ugly
>>>>>>> """
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>>
>

Jesse Eichar
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

They have it in Javafx (which is a statically typed language) and I
have to agree that it is easier to read than printf style formatting
or the + notation. Although the + notation isn't a terrible
compromise. I have not personally experienced problems in Javafx or
Groovy, both of which I have some experience (although not a ton).

Here's a Javafx example:

"Hello {authorOfEmail}"

You can escape the { and } of course.

Jesse
On 17-Mar-09, at 12:38 PM, Ricky Clarkson wrote:

> (assuming you meant to reply to the list, so replying to the list
> myself)
>
> I agree with you.
>
> 2009/3/17 Martin Kuhn :
>> I didn't thought that my simple question leads to such a heavy
>> response.
>>
>> I think most "dynamic" languages out there have such a feature.
>>
>> e.g. Groovy
>> date = new Date(0)
>> out = "Year $date.year Month $date.month Day $date.date"
>> assert out == 'Year 70 Month 0 Day 1'
>> out = "Date is ${date.toGMTString()} !"
>>
>>
>> From my point of view it leads to cleaner source code when you have
>> such a feature (especially when you have to build complex strings
>> with
>> variables)
>>
>>
>>
>>
>>
>> On Tue, Mar 17, 2009 at 12:24 PM, Ricky Clarkson
>> wrote:
>>> I haven't been active in any language community whose language
>>> already
>>> has this feature. So, I'll ask. Does it cause problems for Groovy,
>>> Perl or PHP programmers? I realise Perl at least can eval at
>>> runtime
>>> if necessary, but I don't think it does by default. I don't think
>>> Groovy has that luxury, but I don't know it enough to be sure.
>>>
>>> 2009/3/17 Chris Twiner :
>>>> So the rule would also be, this syntax cannot be used in string
>>>> parameters to functions, its not actually a real string. At the
>>>> very
>>>> least it shouldn't look like a real string then, different dividers
>>>> than " othewise you'll only be answering emails from other users
>>>> as to
>>>> why this string isn't actually a string after desugaring.
>>>>
>>>> On Tue, Mar 17, 2009 at 12:18 PM, Ricky Clarkson
>>>> wrote:
>>>>> I'm really not thinking of this as being eval.
>>>>>
>>>>> "Hello ${name}. How are you today?" should expand, at compile
>>>>> time, to:
>>>>>
>>>>> () => "Hello " + name + ". How are you today?"
>>>>>
>>>>> and when referred to, it should be applied. If name is a val or
>>>>> lazy
>>>>> val, it would be ok to make it a String, not a lambda.
>>>>>
>>>>> 2009/3/17 Chris Twiner :
>>>>>> Ok, I'll bite -> expanded text -> I don't see what it buys you
>>>>>> when
>>>>>> you have no eval syntax possible in scala. I was fairly sure
>>>>>> the rest
>>>>>> of the text in the email made that clear, but apparently not.
>>>>>>
>>>>>> On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson
>>>>>> wrote:
>>>>>>> "I'm not sure what the ${syntax} buys you"
>>>>>>>
>>>>>>> Readability. Not leaving your String literal just to include a
>>>>>>> variable's contents. Not having potential confusion around
>>>>>>> spaces.
>>>>>>> Readability.
>>>>>>>
>>>>>>> println("Hello, ${name}. How are you today?")
>>>>>>>
>>>>>>> paulp, take it away.
>>>>>>>
>>>>>>> 2009/3/17 David MacIver :
>>>>>>>> 2009/3/17 Chris Twiner :
>>>>>>>>> Thats pretty difficult to get from "I'm new in Scala". Its
>>>>>>>>> quite
>>>>>>>>> possible you've come from a perl background or any other
>>>>>>>>> number of
>>>>>>>>> scripting languages.
>>>>>>>>>
>>>>>>>>> The string format family of functions gives similar ability
>>>>>>>>> with alot
>>>>>>>>> more power (i.e. the formatting). Other than that what you
>>>>>>>>> seem to be
>>>>>>>>> asking for is an eval type language, of which scala
>>>>>>>>> definitely isn't.
>>>>>>>>>
>>>>>>>>> Having said that it would be fairly trivial to scan strings
>>>>>>>>> in a
>>>>>>>>> compiler plugin and extract from this syntax. I personally
>>>>>>>>> can't see
>>>>>>>>> the point. The + syntax and the format string functions
>>>>>>>>> cover the
>>>>>>>>> majority of use cases. I'm not sure what the ${syntax} buys
>>>>>>>>> you.
>>>>>>>>
>>>>>>>> """
>>>>>>>> this is a lot of text
>>>>>>>> more text here
>>>>>>>> I don't think this is is very elegant, do you
>>>>>>>> """+chrisTwiner.name+"""?
>>>>>>>> It works fine if """+text.isSmall+""" but quickly gets ugly
>>>>>>>> """
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>>
>>

Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

I have no problem in understanding how this could work, as I wrote in
the 2nd email this is trivial to implement with a plugin.

However in perl etc a main use (at least in the code I maintained /
inherited ) was to use it via eval, ie. have a central place for
strings to be expanded (as constants) and then refer to them (as
references) later in prints etc. This latter use case is not doable
without eval. Which only leaves the case of saving 1 character with +
and normal strings.

I would agree however that the example you gave of """ escaping is
much more convincing, I just haven't got round to replying to it yet,
don't need to now....

On Tue, Mar 17, 2009 at 12:56 PM, David MacIver wrote:
> 2009/3/17 Chris Twiner :
>> So the rule would also be, this syntax cannot be used in string
>> parameters to functions, its not actually a real string.
>
> Of course it could. It would evaluate in the context it was being
> called in. It's a type of String literal, not magic "eval in the local
> context" object.
>

DRMacIver
Joined: 2008-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

2009/3/17 Chris Twiner :
> So the rule would also be, this syntax cannot be used in string
> parameters to functions, its not actually a real string.

Of course it could. It would evaluate in the context it was being
called in. It's a type of String literal, not magic "eval in the local
context" object.

Michael Campbell
Joined: 2009-03-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

Ricky Clarkson wrote:
> The compiler doesn't have to, no. And the IDE can see it as sugar,
> like it should for for-comprehensions.
>

Right. And I'm pretty sure designing a language around what an IDE is
capable of (or not) is probably the wrong way to go. But I'm no
language designer, either.

Johannes Rudolph
Joined: 2008-12-17,
User offline. Last seen 29 weeks 20 hours ago.
Re: Expression placeholder for Strings??

I'm working on a similar function as a library. It currently allows
the formatting of exactly one instance of an object at a time. Current
features are:
* Accessing properties (methods, bean properties)
* Support for converting some types (currently Date,Calendar)
* Support for conditionals (scala.Option, Boolean)
* Expansion of lists with formatting of elements (java.lang.Iterable, Arrays)
* Compilation to bytecodes into an instance of T => String

See some (incomplete) documentation I wrote a while ago at
http://wiki.github.com/jrudolph/bytecode/objectformatter-for-javascala
. When I find some time I will probably make a release. It would
surely be nice if something like that could be integrated into the
Scala compiler where everything could be type-checked properly.

Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Expression placeholder for Strings??

Well.. designing a language around what an IDE is conceptually capable
of is not bad. E.g., in LINQ, the syntax is 'from x in xs select
x.Foo', not 'select x.Foo from xs', because in the first, an IDE can
give meaningful completions if you type 'from x in xs select x.' and
wait.

2009/3/17 Michael Campbell :
> Ricky Clarkson wrote:
>>
>> The compiler doesn't have to, no.  And the IDE can see it as sugar,
>> like it should for for-comprehensions.
>>
>
> Right.  And I'm pretty sure designing a language around what an IDE is
> capable of (or not) is probably the wrong way to go.  But I'm no language
> designer, either.
>
>
>

Russel Winder
Joined: 2009-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

On Tue, 2009-03-17 at 12:08 +0100, Viktor Klang wrote:

>
> Then the IDE and the compiler has to make sure that when you rename a
> field, it updates all strings refering to it, as well as warning for
> spelling errors or unused literals.

Shouldn't human usability of the language rather than machine
implementability be the driver?

BTW Groovy allows arbitrary expressions in the ${...}, it is not just a
variable value replacement.

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Expression placeholder for Strings??
Ah, so it's basically just compiler re-writing.

On Tue, Mar 17, 2009 at 12:09 PM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
The compiler doesn't have to, no.  And the IDE can see it as sugar,
like it should for for-comprehensions.

2009/3/17 Viktor Klang <viktor.klang@gmail.com>:
>
>
> On Tue, Mar 17, 2009 at 12:01 PM, Ricky Clarkson <ricky.clarkson@gmail.com>
> wrote:
>>
>> "I'm not sure what the ${syntax} buys you"
>>
>> Readability.  Not leaving your String literal just to include a
>> variable's contents.  Not having potential confusion around spaces.
>> Readability.
>>
>> println("Hello, ${name}.  How are you today?")
>>
>> paulp, take it away.
>
> Then the IDE and the compiler has to make sure that when you rename a field,
> it updates all strings refering to it, as well as warning for spelling
> errors or unused literals.
>
>>
>> 2009/3/17 David MacIver <david.maciver@gmail.com>:
>> > 2009/3/17 Chris Twiner <chris.twiner@gmail.com>:
>> >> Thats pretty difficult to get from "I'm new in Scala".  Its quite
>> >> possible you've come from a perl background or any other number of
>> >> scripting languages.
>> >>
>> >> The string format family of functions gives similar ability with alot
>> >> more power (i.e. the formatting).  Other than that what you seem to be
>> >> asking for is an eval type language, of which scala definitely isn't.
>> >>
>> >> Having said that it would be fairly trivial to scan strings in a
>> >> compiler plugin and extract from this syntax.  I personally can't see
>> >> the point.  The + syntax and the format string functions cover the
>> >> majority of use cases.  I'm not sure what the ${syntax} buys you.
>> >
>> > """
>> > this is a lot of text
>> > more text here
>> > I don't think this is is very elegant, do you """+chrisTwiner.name+"""?
>> > It works fine if """+text.isSmall+""" but quickly gets ugly
>> > """
>> >
>
>
>
> --
> Viktor Klang
> Senior Systems Analyst
>



--
Viktor Klang
Senior Systems Analyst
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Expression placeholder for Strings??
Russel,

what I mentioned would very clearly assist the human usability, don't you agree?

On Tue, Mar 17, 2009 at 1:36 PM, Russel Winder <russel.winder@concertant.com> wrote:
On Tue, 2009-03-17 at 12:08 +0100, Viktor Klang wrote:

>
> Then the IDE and the compiler has to make sure that when you rename a
> field, it updates all strings refering to it, as well as warning for
> spelling errors or unused literals.

Shouldn't human usability of the language rather than machine
implementability be the driver?

BTW Groovy allows arbitrary expressions in the ${...}, it is not just a
variable value replacement.

--
Russel.
============================================================
Dr Russel Winder                 Partner

Concertant LLP          t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,     f: +44 8700 516 084    voip:  3Arussel [dot] winder [at] ekiga [dot] net" rel="nofollow">sip:russel.winder@ekiga.net
London SW11 1EN, UK.    m: +44 7770 465 077    xmpp: russel@russel.org.uk



--
Viktor Klang
Senior Systems Analyst
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Expression placeholder for Strings??

On Tue, Mar 17, 2009 at 12:23 PM, Ricky Clarkson
wrote:
> Well.. designing a language around what an IDE is conceptually capable
> of is not bad.  E.g., in LINQ, the syntax is 'from x in xs select
> x.Foo', not 'select x.Foo from xs', because in the first, an IDE can
> give meaningful completions if you type 'from x in xs select x.' and
> wait.

You know, I think that being able to read semantics off source from
left to right and top to bottom is pretty helpful for people as well
as for IDEs ;-)

Cheers,

Miles

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Expression placeholder for Strings??


On Tue, Mar 17, 2009 at 1:57 PM, Dave Griffith <dave.l.griffith@gmail.com> wrote:


To forestall more arguing about IDE capabilities, I'll note that IntelliJ
IDEA already does renaming inside interpolated strings for both Groovy and
Ruby, and does renaming inside interpolated XML expressions in Scala.  In
the Groovy case, this functionality has been available for at least 18
months, IIRC.  This is not rocket surgery.

Who's suggesting we have an ill rocket?
i was merely pointing out that IDE assistance would be preferable if such a feature would to be included.
This was under the pretences that it was not merely a compiler rewrite, which, in this case makes alot more sense than wrapping things up in replace-calls.
 


--Dave Griffith
--
View this message in context: http://www.nabble.com/Expression-placeholder-for-Strings---tp22556004p22558462.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Viktor Klang
Senior Systems Analyst
Dave Griffith
Joined: 2009-01-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

To forestall more arguing about IDE capabilities, I'll note that IntelliJ
IDEA already does renaming inside interpolated strings for both Groovy and
Ruby, and does renaming inside interpolated XML expressions in Scala. In
the Groovy case, this functionality has been available for at least 18
months, IIRC. This is not rocket surgery.

--Dave Griffith

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??
BTW there are of course plenty of ways to evaluate such a string at run time. There's a project on java.net whose name I forgot (something to do with bean binding) that uses Expression Language from JSP, and I think you can call it directly to evaluate a string such as "text ${variable}" (although I guess you have to give it the variable bindings). You could define an implicit so you could put your variable bindings into a map and pass to a new method on a string. Similarly, a project that I'm working on (at http://appbuilder.dev.java.net) can evaluate expressions, although the string part of it has to be quoted (e.g. " 'Text' + variable).
But that's off topic because the question was compiler support. I would be for such a syntax, even if it was only allowed in """ strings. And I like the JavaFX syntax of {}.

On Tue, Mar 17, 2009 at 9:05 AM, Viktor Klang <viktor.klang@gmail.com> wrote:


On Tue, Mar 17, 2009 at 1:57 PM, Dave Griffith <dave.l.griffith@gmail.com> wrote:


To forestall more arguing about IDE capabilities, I'll note that IntelliJ
IDEA already does renaming inside interpolated strings for both Groovy and
Ruby, and does renaming inside interpolated XML expressions in Scala.  In
the Groovy case, this functionality has been available for at least 18
months, IIRC.  This is not rocket surgery.

Who's suggesting we have an ill rocket?
i was merely pointing out that IDE assistance would be preferable if such a feature would to be included.
This was under the pretences that it was not merely a compiler rewrite, which, in this case makes alot more sense than wrapping things up in replace-calls.
 


--Dave Griffith
--
View this message in context: http://www.nabble.com/Expression-placeholder-for-Strings---tp22556004p22558462.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Viktor Klang
Senior Systems Analyst

DRMacIver
Joined: 2008-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Expression placeholder for Strings??

2009/3/17 martin odersky :
>> Actually, yes. Even ignoring the fact that I find the ${ } approach a
>> lot more readable (though ${ } would be a poor choice in my opinion as
>> it conflicts with standard templating engines on the JVM), the case
>> where + is a huge pain point is when you have multiline strings.
>> """+expr+""" is suddenly a lot less appealing in comparison.
>>
> True. But """ """ are raw strings without any escapes. It seems weird
> to make an exception for an escape of embedded exceptions but allow no
> other escapes in these strings.

Well, it may be slightly weird, but there's a fair bit of precedent
for the behaviour from scripting languages and it's really useful. I
invite you to refresh your memory of genprod.scala as an example of
the alternative. :-)

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland