3 ms·
D "string mixins" allow to insert any string as code, usually the result of a compile-time computation. Some use cases: - make repetitive code shorter [1]. Th
by p0nce 14y ago
D "string mixins" allow to insert any string as code, usually the result of a compile-time computation.
Some use cases:
- make repetitive code shorter [1]. Think the C preprocessor but after the parsing stage.
- operator overloading is a single template function parameterized by a compile-time string. This more or less requires using string mixins [2].
- [edit] raleks's comment has better examples [3].
Overall string mixins feel a bit dirty but you get used to it. D also has "template mixins" which do not escape parsing prior instantiation.
D mixins are neither like Scala mixins nor Ruby mixins.
[1] https://github.com/p0nce/gfm/blob/master/math/easing.d#L64 https://github.com/p0nce/gfm/blob/master/math/easing.d#L64.
[2] https://github.com/Dav1dde/gl3n/blob/master/gl3n/linalg.d#L2232 https://github.com/Dav1dde/gl3n/blob/master/gl3n/linalg.d#L2...
[3] http://news.ycombinator.com/item?id=4456422 http://news.ycombinator.com/item?id=4456422