Is there a cleaner way of doing this?

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 8 14:18:52 PDT 2017


On 08/08/2017 05:03 AM, Timon Gehr wrote:
> On 08.08.2017 08:06, Shachar Shemesh wrote:
>> On 07/08/17 12:37, Timon Gehr wrote:
>>
>>> struct S(T...) {
>>>      T param;
>>>
>>>      void initialize(T param) {
>>>          this.param = param;
>>>          // Other stuff
>>>      }
>>> }
>>>
>>> Then, use S!() instead of S!void.
>>>
>>
>> It's an interesting approach. It has the down side that it also 
>> accepts S!(int, string, int[23], double), and I'm still not sure what 
>> I think about this option (i.e. - whether I want to allow it).
>>
>> If not, then things start to look quite misleading to the user, and 
>> I'd rather have the ugly static-ifs than do that.
>>
>> Shachar
> 
> I don't see why not, but you can just add a template constraint:
> 
> struct S(T...) if(T.length<=1) { ... }
> 
> 
> You can also hide the approach as an implementation detail:
> 
> struct S(T){
>      static if(is(T==void)){
>          private alias X = AliasSeq!();
>      }else{
>          private alias X = AliasSeq!T;
>      }
>      X param;
>      void initialize(X param){
>          this.param=param;
>          // ...
>      }
> }

Very good creative use of the language, kudos Timon. -- Andrei


More information about the Digitalmars-d mailing list