B Revzin - if const expr isn't broken (was Re: My Meeting C++ Keynote video is now available)
Mark
smarksc at gmail.com
Fri Jan 18 20:03:48 UTC 2019
On Thursday, 17 January 2019 at 20:47:38 UTC, Steven
Schveighoffer wrote:
>
> 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
Why not do away with AliasSeq and use strings all the way?
string Constify(string type)
{
// can add input checks here
return "const(" ~ type ~ ")";
}
void main()
{
import std.algorithm : map;
enum someTypes = ["int", "char", "bool"];
enum constTypes = map!Constify(someTypes);
mixin(constTypes[0] ~ "myConstInt = 42;"); // int myConstInt
= 42;
}
Represent types as strings, CTFE them as you see fit, and output
a string that can then be mixin'ed to use the actual type. :)
More information about the Digitalmars-d-announce
mailing list