www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What are delimited strings good for?

reply simendsjo <simen.endsjo pandavre.com> writes:
Ref http://digitalmars.com/d/2.0/lex.html

What are some possible use cases for delimited strings?
What is solved by having this in the language?
Apr 10 2011
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
 Ref http://digitalmars.com/d/2.0/lex.html
 
 What are some possible use cases for delimited strings?
 What is solved by having this in the language?

It's probably so that you can still use characters that require escaping in the string without having to escape " everywhere. WYSIWYGS don't allow for escaping characters, which can be annoying periodically. Still, delimited strings do seem a bit funny to me, and I don't think that I've ever used them. Presumably, they solved some problem fairly nicely, but the only thing that I can think of is that they still allow for characters that require escaping but don't force you to escape ", which could be very useful in strings with lots of "s and other characters that need escaping. - Jonathan M Davis
Apr 10 2011
parent reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
On 10/04/2011 17:51, Jonathan M Davis wrote:
 Ref http://digitalmars.com/d/2.0/lex.html

 What are some possible use cases for delimited strings?
 What is solved by having this in the language?

It's probably so that you can still use characters that require escaping in the string without having to escape " everywhere. WYSIWYGS don't allow for escaping characters, which can be annoying periodically. Still, delimited strings do seem a bit funny to me, and I don't think that I've ever used them. Presumably, they solved some problem fairly nicely, but the only thing that I can think of is that they still allow for characters that require escaping but don't force you to escape ", which could be very useful in strings with lots of "s and other characters that need escaping. - Jonathan M Davis

DelimitedString: q" Delimiter WysiwygCharacters MatchingDelimiter " Therefore you can choose you delimiter, which as you say might be " Also it looks like you can't use any escapes, because it says WysiwygCharacters above. and further down: Wysiwyg Strings Wysiwyg quoted strings are enclosed by r" and ". All characters between the r" and " are part of the string except for EndOfLine which is regarded as a single \n character. There are no escape sequences inside r" ": I take that to mean that you can't use escapes in DelimitedStrings, either. This would be quite useful for regex, for example.
Apr 10 2011
parent simendsjo <simen.endsjo pandavre.com> writes:
On 10.04.2011 21:17, Andrej Mitrovic wrote:
 Are we forgetting the fact that using delimited strings allows us to
 have syntax highlighting in editors?

 E.g.:

 void stringParser(string str)()
 {
      mixin(str);
 }

 void main()
 {
      stringParser!(q{
          int x = 1;
          int y = 2;

          writefln("%s %s", x, y);
      });
 }

 This is a huge benefit over having everything highlighted in one solid
 color as a string.

That's token strings. This is delimited strings: q"(foo(xxx))" // "foo(xxx)" q"[foo{]" // "foo{"
Apr 10 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
 On 10/04/2011 17:51, Jonathan M Davis wrote:
 Ref http://digitalmars.com/d/2.0/lex.html
 
 What are some possible use cases for delimited strings?
 What is solved by having this in the language?

It's probably so that you can still use characters that require escaping in the string without having to escape " everywhere. WYSIWYGS don't allow for escaping characters, which can be annoying periodically. Still, delimited strings do seem a bit funny to me, and I don't think that I've ever used them. Presumably, they solved some problem fairly nicely, but the only thing that I can think of is that they still allow for characters that require escaping but don't force you to escape ", which could be very useful in strings with lots of "s and other characters that need escaping. - Jonathan M Davis

DelimitedString: q" Delimiter WysiwygCharacters MatchingDelimiter " Therefore you can choose you delimiter, which as you say might be " Also it looks like you can't use any escapes, because it says WysiwygCharacters above. and further down: Wysiwyg Strings Wysiwyg quoted strings are enclosed by r" and ". All characters between the r" and " are part of the string except for EndOfLine which is regarded as a single \n character. There are no escape sequences inside r" ": I take that to mean that you can't use escapes in DelimitedStrings, either. This would be quite useful for regex, for example.

I didn't read carefully enough then. I thought that delimited strings allowed for escaped characters. If they don't, then I really don't understand why they exist. WYSIWYG strings already do that for you. All you have to do is use backticks instead of double quotes. Maybe backticks aren't on all types of keyboards? If so, that might explain the addition, but other than that, the only thing that I can think of is if you want a WYSIWYG string with lots of backticks in it. - Jonathan M Davis
Apr 10 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Are we forgetting the fact that using delimited strings allows us to
have syntax highlighting in editors?

E.g.:

void stringParser(string str)()
{
    mixin(str);
}

void main()
{
    stringParser!(q{
        int x = 1;
        int y = 2;

        writefln("%s %s", x, y);
    });
}

This is a huge benefit over having everything highlighted in one solid
color as a string.
Apr 10 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Hmm..

Well then, those I've never seen used before. :)
Apr 10 2011
prev sibling parent reply Ary Manzana <ary esperanto.org.ar> writes:
On 4/10/11 11:03 AM, simendsjo wrote:
 Ref http://digitalmars.com/d/2.0/lex.html

 What are some possible use cases for delimited strings?
 What is solved by having this in the language?

Readability. auto s = "This is 'something' that \"could\" have been made easier to read"; auto t = q"[This is 'something' that "could" have been made easier to read]"; (although I don't like that you have to type two chars after the q. In ruby you'd write %q(This is 'something' that "could have been made easier to read) )
Apr 10 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
 On 4/10/11 11:03 AM, simendsjo wrote:
 Ref http://digitalmars.com/d/2.0/lex.html
 
 What are some possible use cases for delimited strings?
 What is solved by having this in the language?

Readability. auto s = "This is 'something' that \"could\" have been made easier to read"; auto t = q"[This is 'something' that "could" have been made easier to read]"; (although I don't like that you have to type two chars after the q. In ruby you'd write %q(This is 'something' that "could have been made easier to read) )

WYSIWYG strings already give you that with everything but backticks. So, other than strings with both backticks and quotes in them, I don't see much point to delimited strings. The string that you gave could have been done with auto t = `This is 'something' that "could" have been made easier to read`; If that had been `something instead of 'something, _then_ the delimited string becomes useful, but given how rarely backticks are needed, it does seem rather odd to have delimited strings just for that. So, I definitely have to wonder why they exist. - Jonathan M Davis
Apr 10 2011
next sibling parent Ary Manzana <ary esperanto.org.ar> writes:
On 4/10/11 5:51 PM, Jonathan M Davis wrote:
 On 4/10/11 11:03 AM, simendsjo wrote:
 Ref http://digitalmars.com/d/2.0/lex.html

 What are some possible use cases for delimited strings?
 What is solved by having this in the language?

Readability. auto s = "This is 'something' that \"could\" have been made easier to read"; auto t = q"[This is 'something' that "could" have been made easier to read]"; (although I don't like that you have to type two chars after the q. In ruby you'd write %q(This is 'something' that "could have been made easier to read) )

WYSIWYG strings already give you that with everything but backticks. So, other than strings with both backticks and quotes in them, I don't see much point to delimited strings. The string that you gave could have been done with auto t = `This is 'something' that "could" have been made easier to read`; If that had been `something instead of 'something, _then_ the delimited string becomes useful, but given how rarely backticks are needed, it does seem rather odd to have delimited strings just for that. So, I definitely have to wonder why they exist. - Jonathan M Davis

true
Apr 10 2011
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
On 10/04/2011 21:51, Jonathan M Davis wrote:
<snip>
 If that had been `something instead of 'something, _then_ the delimited string
 becomes useful, but given how rarely backticks are needed, it does seem rather
 odd to have delimited strings just for that. So, I definitely have to wonder
 why they exist.

I guess for those odd occasions when you want to embed large blocks of text/code in your program and not worry about what characters it may contain, because your choice of delimiting token is too contrived to actually appear in the string. It's probably the same reason that Perl has a similar feature. And I can imagine it having metaprogramming uses. Stewart.
Apr 11 2011