Frequently when trying to implement missing language-features in the library, it grinds to a halt because it's not possible to send declarations as template parameters.

i.e. ex!(int i);

Phobos solved this in a for me very undesirable way, ex:
  mixin(bitfields!(uint, "x", 1));
It no longer looks like a D-style declaration.

The obvious alternative is using strings and while I personally actually prefer that to the multiple parameters approach, I know a number of people who are vocally opposed to forcing library end-users to deal with string-based api:s and they do have a point, if only there was a better alternative?

ex!(int, "i"); // oh ze horror!
ex!q{int i};   // at least it now looks like a declaration again.

In before someone says: "Bikeshedding!"

Yes, it's just a few chars, but there's also a bigger difference, type-safety, good error messages and you don't really want to start parsing 'D' for something this trivial, every-time there is a new feature you have to update your little parser, etc. Well, I guess you could mixin the string as a declaration and then reflect over it... maybe it's the best way, didn't try that approach yet.

I did come up with a new(?) way though... as all the other methods above, it has it's ups and downs... but I didn't see any code in the wild using this method before, so I thought I'd share it.

struct Struct
{
  mixin restricted!((uint Size) => 777); // look ma, no strings!
mixin restricted!(uint Size = 777); // not working, "ideal version"
}

A more complete motivating example at asm.dlang.org(just a quick hack not production ready):
http://goo.gl/RSziKx

I guess it could be possible to solve using UDA:s instead... maybe I'll try that next, just checking if I'm the only one dreaming about "declarations as template parameters".


Reply via email to