DIP 50 - AST macros

Simen Kjærås simen.kjaras at gmail.com
Wed Nov 13 02:49:59 PST 2013


On 12.11.2013 18:53, Ellery Newcomer wrote:


> Place.objects.filter(name="Bob's Cafe")
[snip]
> C. it would look like even worse crap in D (no named parameters!)

It's perfectly possible in D to make this work:

     Place.objects.filter(args.name = "Bob", args.state = "unemployed");


Proof of concept:

import std.stdio : writeln;

struct NamedArg(string name, Args...) {
     Args value;
     alias value this;
}

struct args {
     @property
     static auto opDispatch(string name, Args...)(Args args) {
         return NamedArg!(name, Args)(args);
     }
}

void foo(NamedArg!("name", string) a) {
     writeln(a);
}

void main() {
     foo(args.name = "Bob");
}

And since the name is statically known, you can use this for e.g. lookup 
on objects.

No, it's nowhere near as pretty, and the receiving code is horrible. If 
you were to use this for some kind of magic behind the scenes, it might 
be okay. Might. Please don't do it.


> D. it doesn't readily permit you to make queries with complex expression
> trees (idk, maybe something like linq's
>
> Places.Where(p => p.Elev < 200 && Addresses.Any(a => a.State == p.State))

Sorry, I got nuffin'. :p


Waaait...

   Places.where(args.elev.lessThan(200) & args.state =
         (state)=>Addresses.any(state));

I *can* make that work. I'm not going to.

--
   Simen


More information about the Digitalmars-d mailing list