www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What does q{...} mean?

reply "Gary Willoughby" <dev nomad.so> writes:
I keep seeing this syntax used a bit and i'm stumped to what it 
means. What is it?

enum foo = q{
    // ???
};
Feb 24 2014
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby 
wrote:
 I keep seeing this syntax used a bit and i'm stumped to what it 
 means. What is it?

 enum foo = q{
    // ???
 };
http://dlang.org/lex.html#DelimitedString
Feb 24 2014
parent reply "anonymous" <anonymous example.com> writes:
On Monday, 24 February 2014 at 11:47:02 UTC, Namespace wrote:
 On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby 
 wrote:
 I keep seeing this syntax used a bit and i'm stumped to what 
 it means. What is it?

 enum foo = q{
   // ???
 };
http://dlang.org/lex.html#DelimitedString
It's a token string though, not a delimited string. See the section "Token Strings" on that page.
Feb 24 2014
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Monday, 24 February 2014 at 11:53:00 UTC, anonymous wrote:
 On Monday, 24 February 2014 at 11:47:02 UTC, Namespace wrote:
 On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby 
 wrote:
 I keep seeing this syntax used a bit and i'm stumped to what 
 it means. What is it?

 enum foo = q{
  // ???
 };
http://dlang.org/lex.html#DelimitedString
It's a token string though, not a delimited string. See the section "Token Strings" on that page.
Yeah right. It's hard to find quick the right anchors...
Feb 24 2014
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
 On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby wrote:
 I keep seeing this syntax used a bit and i'm stumped to what it means.
 What is it?
 enum foo = q{
  // ???
 };
 It's a token string though, not a delimited string. See the
 section "Token Strings" on that page.
For Gary: the main use is that your IDE / Editor probably does not know about it and hence does not highlight/colour it as a string. If you put to-be-mixed-in code inside this token string, it'll be highlighted as code in editor. It helps catching stoopid mistakes like writing 'imutable i = foo();'.
Feb 24 2014
prev sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Monday, 24 February 2014 at 11:53:00 UTC, anonymous wrote:
 On Monday, 24 February 2014 at 11:47:02 UTC, Namespace wrote:
 On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby 
 wrote:
 I keep seeing this syntax used a bit and i'm stumped to what 
 it means. What is it?

 enum foo = q{
  // ???
 };
http://dlang.org/lex.html#DelimitedString
It's a token string though, not a delimited string. See the section "Token Strings" on that page.
What are they used for? Simpler for creating code at compile time?
Feb 24 2014
parent reply 1100110 <0b1100110 gmail.com> writes:
On 2/24/14, 11:06, Gary Willoughby wrote:
 On Monday, 24 February 2014 at 11:53:00 UTC, anonymous wrote:
 On Monday, 24 February 2014 at 11:47:02 UTC, Namespace wrote:
 On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby wrote:
 I keep seeing this syntax used a bit and i'm stumped to what it
 means. What is it?

 enum foo = q{
  // ???
 };
http://dlang.org/lex.html#DelimitedString
It's a token string though, not a delimited string. See the section "Token Strings" on that page.
What are they used for? Simpler for creating code at compile time?
It allows IDE syntax highlighting and code completion to work with strings that are going to be mixed in. You don't have to use it, in fact there's no difference between this and a normal string. It's just nicer.
Feb 24 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 24 February 2014 at 17:47:55 UTC, 1100110 wrote:
 On 2/24/14, 11:06, Gary Willoughby wrote:
 On Monday, 24 February 2014 at 11:53:00 UTC, anonymous wrote:
 On Monday, 24 February 2014 at 11:47:02 UTC, Namespace wrote:
 On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby 
 wrote:
 I keep seeing this syntax used a bit and i'm stumped to 
 what it
 means. What is it?

 enum foo = q{
 // ???
 };
http://dlang.org/lex.html#DelimitedString
It's a token string though, not a delimited string. See the section "Token Strings" on that page.
What are they used for? Simpler for creating code at compile time?
It allows IDE syntax highlighting and code completion to work with strings that are going to be mixed in. You don't have to use it, in fact there's no difference between this and a normal string. It's just nicer.
Well... it's more than just "looking" nicer: there *are* some differences: For starters, a token string has no "terminating" character: You won't have to chose between " or `: It'll simply end with the "last" }. This means that if your mixin-string contains some strings itself, which itself contains quotes, you should have no problems with it. EG: auto code = q{ foreach(i; 0 .. 10) { writefln(`"hello" %s`, i); } }; Notice how there is nothing special denoting the last "}" as "end of string": It's *just* the end of string because it is the closing brace. The only times I've had "issues" is if you plan to use the "qstring" inside a format (*awesome* for generating code), and the generated code itself contains a formatted write, in which case, you'll need to escape the % => auto code = q{ foreach(i; 0 .. %s) { writefln("hello: %%s", i); } }; mixin(format(code, 10)); Also, a "qstring" can only contain valid D tokens ("token string"). If your mixin string does not adhere some the base D syntaxic rules, you'll get a compilation error. In particular, you'll need balanced bracing/parenthesising, and correctly formed tokens.
Feb 24 2014
next sibling parent 1100110 <0b1100110 gmail.com> writes:
On 2/24/14, 12:40, monarch_dodra wrote:
 Also, a "qstring" can only contain valid D tokens ("token string"). If
 your mixin string does not adhere some the base D syntaxic rules, you'll
 get a compilation error. In particular, you'll need balanced
 bracing/parenthesising, and correctly formed tokens.
Oh, I did not know that. Interesting.
Feb 24 2014
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Monday, 24 February 2014 at 18:40:14 UTC, monarch_dodra wrote:
 Also, a "qstring" can only contain valid D tokens ("token 
 string"). If your mixin string does not adhere some the base D 
 syntaxic rules, you'll get a compilation error. In particular, 
 you'll need balanced bracing/parenthesising, and correctly 
 formed tokens.
And even with all these rules, it still works great for other languages like Lua.
Feb 24 2014
prev sibling parent Dejan Lekic <dejan.lekic gmail.com> writes:
Gary Willoughby wrote:

 I keep seeing this syntax used a bit and i'm stumped to what it
 means. What is it?
 
 enum foo = q{
     // ???
 };
q{} string literals (so-called "token strings") are a nice D feature - D garantees that tokens in between brackets are valid D tokens. This makes them perfect for storing D source code. They are quite useful for string mixins for an example. -- http://dejan.lekic.org
Feb 24 2014