digitalmars.D.learn - What does q{...} mean?
- Gary Willoughby (5/5) Feb 24 2014 I keep seeing this syntax used a bit and i'm stumped to what it
- Namespace (3/8) Feb 24 2014 http://dlang.org/lex.html#DelimitedString
- anonymous (3/12) Feb 24 2014 It's a token string though, not a delimited string. See the
- Namespace (2/15) Feb 24 2014 Yeah right. It's hard to find quick the right anchors...
- Philippe Sigaud (5/13) Feb 24 2014 For Gary: the main use is that your IDE / Editor probably does not
- Gary Willoughby (2/15) Feb 24 2014 What are they used for? Simpler for creating code at compile time?
- 1100110 (6/21) Feb 24 2014 It allows IDE syntax highlighting and code completion to work with
- monarch_dodra (32/57) Feb 24 2014 Well... it's more than just "looking" nicer: there *are* some
- 1100110 (2/6) Feb 24 2014 Oh, I did not know that. Interesting.
- Jesse Phillips (3/8) Feb 24 2014 And even with all these rules, it still works great for other
- Dejan Lekic (7/13) Feb 24 2014 q{} string literals (so-called "token strings") are a nice D feature - D...
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
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
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:It's a token string though, not a delimited string. See the section "Token Strings" on that page.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
On Monday, 24 February 2014 at 11:53:00 UTC, anonymous wrote:On Monday, 24 February 2014 at 11:47:02 UTC, Namespace wrote:Yeah right. It's hard to find quick the right anchors...On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby wrote:It's a token string though, not a delimited string. See the section "Token Strings" on that page.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
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{ // ??? };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();'.It's a token string though, not a delimited string. See the section "Token Strings" on that page.
Feb 24 2014
On Monday, 24 February 2014 at 11:53:00 UTC, anonymous wrote:On Monday, 24 February 2014 at 11:47:02 UTC, Namespace wrote:What are they used for? Simpler for creating code at compile time?On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby wrote:It's a token string though, not a delimited string. See the section "Token Strings" on that page.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
On 2/24/14, 11:06, Gary Willoughby wrote:On Monday, 24 February 2014 at 11:53:00 UTC, anonymous wrote: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.On Monday, 24 February 2014 at 11:47:02 UTC, Namespace wrote:What are they used for? Simpler for creating code at compile time?On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby wrote:It's a token string though, not a delimited string. See the section "Token Strings" on that page.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
On Monday, 24 February 2014 at 17:47:55 UTC, 1100110 wrote:On 2/24/14, 11:06, Gary Willoughby wrote: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.On Monday, 24 February 2014 at 11:53:00 UTC, anonymous wrote: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.On Monday, 24 February 2014 at 11:47:02 UTC, Namespace wrote:What are they used for? Simpler for creating code at compile time?On Monday, 24 February 2014 at 11:41:41 UTC, Gary Willoughby wrote:It's a token string though, not a delimited string. See the section "Token Strings" on that page.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
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
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
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