News for hackers. Stuff that matters.
Re:Ownership and Borrowing in D by coder543 on Tuesday July 16, 2019 @02:57AM (#20448031)
Rust’s macro system is incredibly powerful — much more powerful than C++’s, which is just text substitution. It’s easy to make DSLs in Rust. That’s not what I’m talking about.

> Well, the alternative is to learn an actual dozen of language

That’s a false dichotomy.

Re:Ownership and Borrowing in D by pjmlp on Tuesday July 16, 2019 @07:41AM (#20449149)
C++ also has template metaprogramming and compile time execution nowadays.

Common wisdom is to leave macros for conditional execution, legacy code and the few cases that still aren't fully designed, namely reflection and metaclasses.

Re:Ownership and Borrowing in D by fluffything on Tuesday July 16, 2019 @03:12AM (#20448082)
Rust macro system is much more powerful than D mixins.

I personally find Rust macro system infinitely easier to use than D mixins as well. In D, I need to learn a new pseudo-language to work with mixins, but Rust macros are just normal Rust code that gets executed at compile-time on other Rust code, and this code can do anything that any running Rust program can do.

The structure of the 2 fundamental Rust libraries around Rust macros are super intuitive to me (maybe its the CS background?). `syn` is a Rust parser from tokens -> AST, and it supports doing AST->AST folds and other common operations. And `quote` gives you semi-quoting.

With `syn+quote` most macros end up as 10 liners. Tokens->AST->AST fold->Quote->Tokens.

Re:Ownership and Borrowing in D by floatboth on Tuesday July 16, 2019 @05:12AM (#20448567)
Huh? D metaprogramming is much closer to "normal code executed at compile time".

Rust procedural macros are like external tools, manipulating the AST as a structure. In D, compile time code is seamlessly interleaved with other code – you just have `static if`, `static foreach` etc. in your code.

I'm not sure what you mean by "new pseudo-language to work with mixins". The term "mixin" is unfortunately overloaded: the `mixin()` call just splices a string into the code, while `template mixin` is a way to expand a template where you want it. Neither introduces new complex structures.

Re:Ownership and Borrowing in D by kbenson on Tuesday July 16, 2019 @10:50AM (#20450588)
> Rust procedural macros are like external tools, manipulating the AST as a structure.

Isn't viewing them as like external tools just a C centric view, based on C's (and C++'s) capabilities? Didn't Lisp have macros that manipulated the language as an AST prior to C even existing, inside the language?

Re:Ownership and Borrowing in D by floatboth on Tuesday July 16, 2019 @12:35PM (#20451585)
Well, Rust is closer to C++ than Lisp :)

If you look at the API https://blog.rust-lang.org/2018/12/21/Procedural-Macros-in-R...

it works on the level of raw tokens, not even the AST. So you have to construct your own AST via the parser if you want to manipulate AST.

Re:Ownership and Borrowing in D by Zardoz84 on Tuesday July 16, 2019 @11:52AM (#20451208)
I don't know Rust macros, but I D had a old example that generates an image, using raycasting, at compile time.