digitalmars.D - std.traits : Select - could it be better?
- SrMordred (11/11) Jul 05 2018 alias U = Select!( isPointer!T, PointerTarget!T, T );
- Andrei Alexandrescu (4/19) Jul 05 2018 mixin("alias U = " ~ Select!(isPointer!T, "PointerTarget!T", "T") ~ ";")...
- Steven Schveighoffer (17/32) Jul 05 2018 Hm... only thing I can think of is to do the instantiation within the
- Steven Schveighoffer (5/13) Jul 05 2018 ugh, got too cute for my own good :) No need for Instantiate here:
- SrMordred (9/24) Jul 05 2018 I find another solution :)
- Steven Schveighoffer (3/13) Jul 06 2018 I like it!
alias U = Select!( isPointer!T, PointerTarget!T, T ); This don´t compile if T are not a pointer; so you have to do this: static if( isPointer!T ) alias U = PointerTarget!T; else alias U = T; Shouldnt the 'correct' way of Select to work is ignoring the choice that was not taken? I love the idea of Select, but because of this I almost never use it.
Jul 05 2018
On 7/5/18 2:50 PM, SrMordred wrote:alias U = Select!( isPointer!T, PointerTarget!T, T ); This don´t compile if T are not a pointer; so you have to do this: static if( isPointer!T ) alias U = PointerTarget!T; else alias U = T; Shouldnt the 'correct' way of Select to work is ignoring the choice that was not taken? I love the idea of Select, but because of this I almost never use it.mixin("alias U = " ~ Select!(isPointer!T, "PointerTarget!T", "T") ~ ";"); Ehm this is actually not better :o). Andrei
Jul 05 2018
On 7/5/18 2:50 PM, SrMordred wrote:alias U = Select!( isPointer!T, PointerTarget!T, T ); This don´t compile if T are not a pointer; so you have to do this: static if( isPointer!T ) alias U = PointerTarget!T; else alias U = T; Shouldnt the 'correct' way of Select to work is ignoring the choice that was not taken? I love the idea of Select, but because of this I almost never use it.Hm... only thing I can think of is to do the instantiation within the template itself: template BetterSelect(bool cond, alias temp1, alias temp2, Args...) { import std.meta : Instantiate; static if(cond) alias BetterSelect = Instantiate!(temp1, Args); else alias BetterSelect = Instantiate!(temp2, Args); } alias U = BetterSelect!(isPointer!T, PointerTarget, Alias, T) Of course, this requires you to have templates that take EXACTLY the same parameters. I don't know if this (or something like it) is worth adding to std.traits or std.meta. -Steve
Jul 05 2018
On 7/5/18 4:27 PM, Steven Schveighoffer wrote:template BetterSelect(bool cond, alias temp1, alias temp2, Args...) { import std.meta : Instantiate; static if(cond) alias BetterSelect = Instantiate!(temp1, Args); else alias BetterSelect = Instantiate!(temp2, Args); }ugh, got too cute for my own good :) No need for Instantiate here: alias BetterSelect = temp1!Args; alias BetterSelect = temp2!Args; -Steve
Jul 05 2018
On Thursday, 5 July 2018 at 20:29:02 UTC, Steven Schveighoffer wrote:On 7/5/18 4:27 PM, Steven Schveighoffer wrote:I find another solution :) template Try(alias T, Args...) { static if( is( T!Args ) ) alias Try = T!Args; } alias U = Select!( isPointer!T, Try!( PointerTarget, T ), T );template BetterSelect(bool cond, alias temp1, alias temp2, Args...) { import std.meta : Instantiate; static if(cond) alias BetterSelect = Instantiate!(temp1, Args); else alias BetterSelect = Instantiate!(temp2, Args); }ugh, got too cute for my own good :) No need for Instantiate here: alias BetterSelect = temp1!Args; alias BetterSelect = temp2!Args; -Steve
Jul 05 2018
On 7/5/18 10:17 PM, SrMordred wrote:I find another solution :) template Try(alias T, Args...) { static if( is( T!Args ) ) alias Try = T!Args; } alias U = Select!( isPointer!T, Try!( PointerTarget, T ), T );I like it! -Steve
Jul 06 2018