November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rob T | On 2013-11-11 02:08, Rob T wrote: > A scalable and elegant solution to the "inspection" problem may be a AST > use case. > > Discussed here: > http://forum.dlang.org/thread/qconpedgdkyeawmdzect@forum.dlang.org Please, feel free to add it as an example, if your interested. After all, it is a wiki. -- /Jacob Carlborg |
November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | On 2013-11-11 02:40, Timothee Cour wrote: > <[ .. ]> syntax looks very foreign. How about m{...} (with q{...} as a > precedent)? As the DIP says, that just one suggestion of the syntax. If we even need a syntax. An alternative, without any new syntax is the "ast" macro: http://wiki.dlang.org/DIP50#The_AST_Macro This is similar to the "refiy" macro in Scala. -- /Jacob Carlborg |
November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Sunday, 10 November 2013 at 21:20:34 UTC, Jacob Carlborg wrote:
> I've been thinking quite long of how AST macros could look like in D. I've been posting my vision of AST macros here in the newsgroup a couple of times already. I've now been asked to create a DIP out of it, so here it is:
>
> http://wiki.dlang.org/DIP50
One of our targets for AST macros should be the ability to replicate roughly linq from c# / .net.
An example syntax for use with AST could be:
auto data = [5, 7, 9];
int[] data2;
query {
from value in data
where value >= 6
add to data2
}
Could be unwrapped to:
auto data = [5, 7, 9];
int[] data2;
foreach(value; data) {
if (value >= 6) data2 ~= value;
}
This isn't a thought out design but it should at least be a target or a possibility.
Also c#'s get set should be rather similar as well.
Example:
getset {
public int id;
private bool exit;
}
Would translate to:
private int id;
private bool exit;
@property {
void id(int v) {this.id = v;}
void exit(bool v) { this.exit = v; }
int id() { return this.id; }
bool exit() { return this.exit; }
}
This would definitely open new possibilities up.
Disclaimer I don't like c# or .net but I am partial to these features.
At current point I think the DIP does have the necessary features to implement this. However it would be nice for safety to be able to get all scope variables of where the macro was initiated from. Being able to check for if a variable exists could provide much needed compile safety and better error messages.
|
November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 2013-11-11 02:46, bearophile wrote: > It's also useful to take a look at what F# is doing: > http://tomasp.net/blog/2013/computation-zoo-padl/index.html I'll do that. I've been looking at several languages, mainly Scala. But I have not looked at F#. -- /Jacob Carlborg |
November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | On 2013-11-11 02:49, Timothee Cour wrote: > People have shunned proposals to have @mixin functions because it > wouldn't be obvious at call site that some statement is executed under a > mixin (which could access all variables in scope etc). > > The same will happen here; I think it should be clear at call site that > a macro is used. > How about: > > macro!myAssert(1 + 2 == 4); > instead of myAssert(1 + 2 == 4); I would really prefer if we not had to do that. -- /Jacob Carlborg |
November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to Shammah Chancellor | On 2013-11-11 04:04, Shammah Chancellor wrote: > Not a huge fan of the syntax, but I think it's a step in the right > direction away from string mixins. To you mean the <[ ]> syntax or something else? See my reply to Timothee: http://forum.dlang.org/thread/l5otb1$1dhi$1@digitalmars.com?page=2#post-l5q1vh:242c2k:241:40digitalmars.com -- /Jacob Carlborg |
November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On 10.11.2013. 22:20, Jacob Carlborg wrote:
> I've been thinking quite long of how AST macros could look like in D. I've been posting my vision of AST macros here in the newsgroup a couple of times already. I've now been asked to create a DIP out of it, so here it is:
>
> http://wiki.dlang.org/DIP50
>
Thumbs up!
|
November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | On 2013-11-11 08:46, Rikki Cattermole wrote: > One of our targets for AST macros should be the ability to replicate > roughly linq from c# / .net. > > An example syntax for use with AST could be: > > auto data = [5, 7, 9]; > int[] data2; > query { > from value in data > where value >= 6 > add to data2 > } > > Could be unwrapped to: > > auto data = [5, 7, 9]; > int[] data2; > foreach(value; data) { > if (value >= 6) data2 ~= value; > } > > This isn't a thought out design but it should at least be a target or a > possibility. Absolutely. One of my favorite examples is the database query: auto person = Person.where(e => e.name == "John"); Which translates to the following SQL: select * from person where name = 'John' > Also c#'s get set should be rather similar as well. > Example: > > getset { > public int id; > private bool exit; > } > > Would translate to: > > private int id; > private bool exit; > @property { > void id(int v) {this.id = v;} > void exit(bool v) { this.exit = v; } > int id() { return this.id; } > bool exit() { return this.exit; } > } > > This would definitely open new possibilities up. > Disclaimer I don't like c# or .net but I am partial to these features. That's quite similar one of the examples, I like to call it "property shortcut": http://wiki.dlang.org/DIP50#Attribute_macros > At current point I think the DIP does have the necessary features to > implement this. However it would be nice for safety to be able to get > all scope variables of where the macro was initiated from. Being able to > check for if a variable exists could provide much needed compile safety > and better error messages. Why not? There's quite a lot that is not specified in this DIP. Mostly because I haven't decided/figured out how it should work exactly. Of course, any help is always appreciated. -- /Jacob Carlborg |
November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | On 2013-11-11 09:08, luka8088 wrote: > Thumbs up! Thanks. -- /Jacob Carlborg |
November 11, 2013 Re: DIP 50 - AST macros | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sunday, 10 November 2013 at 22:33:34 UTC, bearophile wrote:
> Jacob Carlborg:
>
>> http://wiki.dlang.org/DIP50
>
> I suggest to add some more use cases (possibly with their implementation).
>
> Bye,
> bearophile
I agree examples would help a lot. Trying to define what information actually exists within these types would also help a lot.
In the first example, would Ast!(bool) be something like this?
opBinary!"=="
left = opBinary!"+"
left = Literal
type = int
value = 1
right = Literal
type = int
value = 2
right = Literal
type = int
value = 4
Would there be helpers for matching part of the structure?
The same applies to the other types used - what information should they have?
As for examples, here's a couple of suggestions:
* Expression to prefix notation
* Expression to SQL
* AutoImplement properties (like C#)
* Number intervals, like "int i = int[10..20];" where only 10 to 20 are legal values
* Discriminated union
* Pattern matching
|
Copyright © 1999-2021 by the D Language Foundation