5 ms·
Mixins, at least in D, can be very useful when you want to do some meta-programming (at compile-time, of course). They basically allow you to generate code at c
by raleks 14y ago
Mixins, at least in D, can be very useful when you want to do some meta-programming (at compile-time, of course). They basically allow you to generate code at compile time.
Mixins, together with some other cool D features, like CTFE (compile-time function execution), templates and ability to load external file at compile-time can be used to create DSL compiler which compiles to native code (first to D, than to native, of course).
For example, ctRegex from std.regex [1] (standard library regular expressions module) uses mixins to generate D code in compile-time out of regular expression (and D compiler outputs native code for regular expression matching).
There is also a PEG parser that works at compile-time (reads external PEG grammar and generates parser code) but I cannot find a link at the moment.
Of course, creating DSL parsers is not the only thing you can do with mixins, but they are surly the first to come to mind..
[1] https://github.com/D-Programming-Language/phobos/blob/master/std/regex.d https://github.com/D-Programming-Language/phobos/blob/master...