On 04/08/2017 03:11 AM, biocyberman wrote: > On Saturday, 8 April 2017 at 10:02:01 UTC, Mike Parker wrote: >> >> I would expect if you implement it as a function the compiler will >> inline it. You can always use the pragma(inline, true) [1] with >> -inline to verify. >> >> [1] https://dlang.org/spec/pragma.html#inline > > Thanks for mentioning pragma. However, anyway to do it with mixin? It's > so cool so I want to do more stuffs with it :)
You can mixin declarations with a template but I don't see how it can help here. A string mixin would work but it's really ugly at the use site:
string roundUp(alias x)() if (is (typeof(x) == uint)) { import std.string : format; return format(q{ --%1$s; %1$s |= %1$s >> 1; %1$s |= %1$s >> 2; %1$s |= %1$s >> 4; %1$s |= %1$s >> 8; %1$s |= %1$s >> 16; ++%1$s; }, x.stringof); } void main() { uint i = 42; mixin (roundUp!i); // <-- Ugly assert(i == 64); } Compare that to the following natural syntax that a function provides: void roundUp(ref uint x) { // ... } void main() { uint i = 42; i.roundUp(); // <-- Natural } Ali