News for hackers. Stuff that matters.
Re:The reference D compiler is now open source by jordigh on Friday April 07, 2017 @01:15PM (#14061545)
Yeah, I really love that at compile time you can meta-program in almost the same language that you use for normal run-time things. I think the comparison to lisp is apt. It almost feels like lisp macros.

Think C++ template metaprogramming but much, much easier and thus, seemingly more powerful. It's not that you couldn't do the same in C++, but D's metaprogramming is so much more accessible that it makes you want to use it.

Re:The reference D compiler is now open source by jacques_chirac on Friday April 07, 2017 @01:18PM (#14061569)
There's plenty you can do with D but not C++, see regex, bitfields, swap member-by-member with checks for mutual pointers, the pegged library for grammars, etc etc.
Re:The reference D compiler is now open source by jordigh on Friday April 07, 2017 @01:34PM (#14061726)
I don't know, Boost has me convinced that even things like regexes in C++ templates could be possible, if your compiler has a big enough stack to handle very deep template recursions. It's theoretically possible to build a regex parser in C++ templates, right? It would be horrible, but possible.
Re:The reference D compiler is now open source by qznc on Friday April 07, 2017 @01:57PM (#14061978)
With D it is possible and not horrible and already done. ;)

Talk: http://dconf.org/2014/talks/olshansky.html

Re:The reference D compiler is now open source by humanrebar on Friday April 07, 2017 @02:39PM (#14062377)
> It's theoretically possible to build a regex parser in C++ templates, right?

You'd probably do large portions of it in "constexpr" types and functions rather than relying on recursive metaprogramming techniques.

They are also planning on adding overloading based on whether a function is constexpr, which is important if you want the same library to support both compile-time and run-time matchers.

So it's all theoretically possible already. But D has the advantage here in that it's not just possible but usable.

Re:The reference D compiler is now open source by dbcurtis on Friday April 07, 2017 @01:26PM (#14061651)
For someone who knows zero about D, but is a total Pythonista, how would you compare the meta-programming facilities?
Re:The reference D compiler is now open source by jordigh on Friday April 07, 2017 @01:31PM (#14061692)
I guess the best comparison to Python would be the dunder methods. You know how in Python a class really is just a dict, right? Like, foo.bar is pretty much syntactic sugar for foo.__get_attribute__(foo.__dict__['bar']) in most cases. Or how a + b is sugar for a.__plus__(b).

Well, imagine that all of the things you can do in Python by messing with dunder methods, function decorators, or metaclasses could be precomputed by a compiler and would not even execute at all during runtime. It makes everything really fast at runtime and easy to precompute at compile time.

Re:The reference D compiler is now open source by gmfawcett on Friday April 07, 2017 @01:48PM (#14061879)
Yes, and plus you get type safety with all of that! When I'm writing in D, my editor marks all the type errors (and typos) in my code, almost as fast as I make them. The DMD compiler is so quick that there's almost no lag. This leads not only to a rapid development cycle, but a much higher level of reassurance (than I've had when writing, e.g., metaclass hacks in Python).
Re:The reference D compiler is now open source by dbcurtis on Friday April 07, 2017 @02:20PM (#14062214)
Well, overriding dunder methods isn't really metaprogramming. It's just method inheritance/override.

Is there anything in D similar to decorators? How does the registration pattern work in D, for instance?

Re:The reference D compiler is now open source by gmfawcett on Friday April 07, 2017 @02:34PM (#14062327)
Do you mean like this kind of registration decorator?

https://github.com/rejectedsoftware/vibe.d/blob/master/examp...

Decorators (in D, 'user defined attributes', or UDA) work differently. It's not a function-composition feature, but more of a tagging feature. You write a class, function, etc., tag it with custom attributes; and then have a separate compile-time function walk over your code, find the decorators, and augment the code based the meaning of the tags. (I used to wish that D had adopted Python-style decorators, since they are easy to reason about and implement, but I can see the logic of the more general UDA system that they adopted.)

In practice, though, you would often use templates to achieve the same effect. Given a memoize template (really, just a memoize function), and an expensive-computation function,

    auto fastComputer = memoize!expensiveComputer;
produces roughly the equivalent of

    @memoize
    def expensiveComputer(): ...
but with opportunities for compile-time optimization.