Thread overview | |||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 22, 2017 Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
http://dlang.org/blog/2017/05/22/introspection-introspection-everywhere/ -- Andrei |
May 22, 2017 Re: Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 05/22/2017 11:05 AM, Andrei Alexandrescu wrote: > http://dlang.org/blog/2017/05/22/introspection-introspection-everywhere/ -- Andrei Submitted to reddit as well: https://www.reddit.com/r/programming/comments/6cntso/from_the_d_blog_introspection_introspection/ -- Andrei |
May 22, 2017 Re: Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 22 May 2017 at 15:05:24 UTC, Andrei Alexandrescu wrote: > mixin is a statement so it needs a terminator, hence > the semicolon at the very end. In turn, mixin takes > a string (the concatenation of variable op That actually depends on context! The mixin statement needs statements, but the mixin expression takes expressions. Same mixin keyword, but if it is in the context of a statement it is a statement and in the context of an expression, it is an expression. --- void main() { int payload; mixin("++payload;"); // statement, ; required int b = mixin("++payload"); // expression, ; would be wrong there and cause an error! } --- http://dlang.org/spec/expression.html#MixinExpression http://dlang.org/spec/statement.html#MixinStatement |
May 22, 2017 Re: Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 05/22/2017 11:23 AM, Adam D. Ruppe wrote:
> On Monday, 22 May 2017 at 15:05:24 UTC, Andrei Alexandrescu wrote:
>> mixin is a statement so it needs a terminator, hence
>> the semicolon at the very end. In turn, mixin takes
>> a string (the concatenation of variable op
>
> That actually depends on context! The mixin statement needs statements, but the mixin expression takes expressions. Same mixin keyword, but if it is in the context of a statement it is a statement and in the context of an expression, it is an expression.
>
> ---
> void main() {
> int payload;
> mixin("++payload;"); // statement, ; required
> int b = mixin("++payload"); // expression, ; would be wrong there and cause an error!
> }
> ---
>
> http://dlang.org/spec/expression.html#MixinExpression
> http://dlang.org/spec/statement.html#MixinStatement
Yah, didn't want to overload the article (or the discussion) with the expression/statement distinction. -- Andrei
|
May 22, 2017 Re: Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 05/22/2017 11:26 AM, Andrei Alexandrescu wrote:
> On 05/22/2017 11:23 AM, Adam D. Ruppe wrote:
>> On Monday, 22 May 2017 at 15:05:24 UTC, Andrei Alexandrescu wrote:
>>> mixin is a statement so it needs a terminator, hence
>>> the semicolon at the very end. In turn, mixin takes
>>> a string (the concatenation of variable op
>>
>> That actually depends on context! The mixin statement needs statements, but the mixin expression takes expressions. Same mixin keyword, but if it is in the context of a statement it is a statement and in the context of an expression, it is an expression.
>>
>> ---
>> void main() {
>> int payload;
>> mixin("++payload;"); // statement, ; required
>> int b = mixin("++payload"); // expression, ; would be wrong there and cause an error!
>> }
>> ---
>>
>> http://dlang.org/spec/expression.html#MixinExpression
>> http://dlang.org/spec/statement.html#MixinStatement
>
> Yah, didn't want to overload the article (or the discussion) with the expression/statement distinction. -- Andrei
Updated the post, it's an interesting point after all. Thanks! -- Andrei
|
May 22, 2017 Re: Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 22 May 2017 at 15:26:26 UTC, Andrei Alexandrescu wrote:
> Yah, didn't want to overload the article (or the discussion) with the expression/statement distinction. -- Andrei
Yeah, the details might be too much for a general audience (and I realize you know this, I'm commenting more for other readers who might be interested), but I do think this illustrates an important point for anyone who wants to use mixin: it is similar to, but not exactly like copy/pasting code into the source.
mixin parses a piece of code, then pastes the *AST node*, not the source code, into the tree where the mixin is found. That's why the semicolon is required in the statement context - it needs a complete branch that actually fits the AST at the moment, not just a string that is pasted into the source code at the location.
So similar enough to copy/paste to get someone quickly started playing with code generation, but this key difference is needed to really understand it.
|
May 22, 2017 Re: Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 22 May 2017 at 15:05:24 UTC, Andrei Alexandrescu wrote:
> http://dlang.org/blog/2017/05/22/introspection-introspection-everywhere/ -- Andrei
It was really an entertaining and interesting post, thank you.
|
May 22, 2017 Re: Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 22 May 2017 at 15:12:42 UTC, Andrei Alexandrescu wrote:
> On 05/22/2017 11:05 AM, Andrei Alexandrescu wrote:
>> http://dlang.org/blog/2017/05/22/introspection-introspection-everywhere/ -- Andrei
>
> Submitted to reddit as well: https://www.reddit.com/r/programming/comments/6cntso/from_the_d_blog_introspection_introspection/ -- Andrei
Perfect! I have been in Weka.IO in this january too! Big thanks guys!
|
May 22, 2017 Re: Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On 22.05.2017 17:38, Adam D. Ruppe wrote:
> On Monday, 22 May 2017 at 15:26:26 UTC, Andrei Alexandrescu wrote:
>> Yah, didn't want to overload the article (or the discussion) with the
>> expression/statement distinction. -- Andrei
>
> Yeah, the details might be too much for a general audience (and I
> realize you know this, I'm commenting more for other readers who might
> be interested), but I do think this illustrates an important point for
> anyone who wants to use mixin: it is similar to, but not exactly like
> copy/pasting code into the source.
>
> mixin parses a piece of code, then pastes the *AST node*, not the source
> code, into the tree where the mixin is found. That's why the semicolon
> is required in the statement context - it needs a complete branch that
> actually fits the AST at the moment, not just a string that is pasted
> into the source code at the location.
>
> So similar enough to copy/paste to get someone quickly started playing
> with code generation, but this key difference is needed to really
> understand it.
The grammar has:
expression;
as a statement.
mixin(...)
as an expression
and
mixin(...);
as a statement
Hence,
mixin(...);
is actually grammatically ambiguous and the behaviour of the compiler is somewhat arbitrary.
The compiler could easily compensate for the ambiguity during semantic and not require the terminating semicolon when parsing the string of a mixin statement.
|
May 22, 2017 Re: Trip notes from Israel | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Monday, 22 May 2017 at 15:05:24 UTC, Andrei Alexandrescu wrote:
> http://dlang.org/blog/2017/05/22/introspection-introspection-everywhere/ -- Andrei
Wow, that was really good. Love to read more of your trip notes.
|
Copyright © 1999-2021 by the D Language Foundation