Dlang Features You Would Like To Share

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 12 14:57:08 PDT 2017


On Wednesday, 12 April 2017 at 21:40:48 UTC, bluecat wrote:
> What are some features that you have discovered that you would 
> like to share with the community? For me, one thing I found 
> interesting was the ability to define structures dynamically 
> using mixins:
>
> import std.stdio;
> import std.format: format;
>
> template MakePoint(string name, string x, string y) {
>     const char[] MakePoint = "struct %s {int %s; int 
> %s;}".format(name, x, y);
> }
>
> mixin(MakePoint!("Point", "x", "y"));
>
> void main() {
>     auto pt = new Point;
>     pt.x = 1;
>     pt.y = 2;
>     writefln("point at (%s, %s)", pt.x, pt.y);
> }

this one does not need to be a template.
You can build the string using ctfe which is much faster indeed.


More information about the Digitalmars-d mailing list