B Revzin - if const expr isn't broken (was Re: My Meeting C++ Keynote video is now available)

Steven Schveighoffer schveiguy at gmail.com
Thu Jan 17 20:47:38 UTC 2019


On 1/17/19 2:31 PM, H. S. Teoh wrote:
> On Thu, Jan 17, 2019 at 06:03:07PM +0000, Paul Backus via Digitalmars-d-announce wrote:
> [...]
>> [2]
>> https://bartoszmilewski.com/2009/10/21/what-does-haskell-have-to-do-with-c/
> [...]
> 
> Haha, seems D did better than C++ in this respect, but not quite at the
> level of Haskell.
> 
> The C++ example of a template that takes templates and arguments and
> declares another template is a perfect example of why C++ template
> syntax is utterly horrible for doing these sorts of things.
> 
> Coming back to the D example at the end, I totally agree with the
> sentiment that D templates, in spite of their significant improvements
> over C++ syntax, ultimately still follow the same recursive model. Yes,
> you can use CTFE to achieve the same thing at runtime, but it's not the
> same thing, and CTFE cannot manipulate template argument lists (aka
> AliasSeq aka whatever it is you call them).  This lack of symmetry
> percolates down the entire template system, leading to the necessity of
> the hack that Bartosz refers to.
> 
> Had template argument lists / AliasSeq been symmetric w.r.t. runtime
> list manipulation, we would've been able to write a foreach loop that
> manipulates the AliasSeq in the most readable way without needing to
> resort to hacks or recursive templates.

well, there was no static foreach for that article (which I admit I 
didn't read, but I know what you mean).

But it's DEFINITELY not as easy as it could be:

import std.conv;

alias AliasSeq(P...) = P;

template staticMap(alias Transform, Params...)
{
     alias seq0 = Transform!(Params[0]);
     static foreach(i; 1 .. Params.length)
     {
        mixin("alias seq" ~ i.to!string ~ " = AliasSeq!(seq" ~ 
(i-1).to!string ~ ", Transform!(Params[" ~ i.to!string ~ "]));");
     }
     mixin("alias staticMap = seq" ~ (Params.length-1).to!string ~ ";");
}

alias Constify(T) = const(T);
void main()
{
     alias someTypes = AliasSeq!(int, char, bool);
     pragma(msg, staticMap!(Constify, someTypes)); // (const(int), 
const(char), const(bool))
}

Note, that this would be a LOT easier with string interpolation...

mixin("alias seq${i} = AliasSeq!(seq${i-1}, 
Transform!(Params[${i}]));".text);

-Steve


More information about the Digitalmars-d-announce mailing list