non empty slices

Alex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 2 14:37:21 PDT 2016


On Thursday, 2 June 2016 at 20:11:21 UTC, Alex wrote:
> On Thursday, 2 June 2016 at 16:21:03 UTC, ag0aep6g wrote:
>>     void f(int[] arr)
>>     {
>>         A a = arrayToA(arr);
>>         foreach (T; A.AllowedTypes)
>>         {
>>             if (T* p = a.peek!T) f_impl(*p);
>>         }
>>     }
> You totally hit the point!
> The cool thing about the Algebraic is as I expected, that it 
> doesn't change it's type... And the hard thing is, that I'm not 
> used to its Empty, Many, ... things yet.
> But the question remains how to keep this @nogc? I wonder at 
> the line with peek... and why it is not just returning the 
> value...

Just tried this instead of your f-function:
void f(int[] arr)
{
     A result;
     import std.meta;
     alias TL = AliasSeq!(Empty, int, Many!int);
     int caseS;
     switch (arr.length)
     {
         case 0: result = Empty.init; caseS = 0; break;
         case 1: result = arr[0]; caseS = 1;  break;
         default: result = Many!int(arr); caseS = 2;
     }
     f_impl(*result.get!(TL[caseS]));
}
But got: Error: variable caseS cannot be read at compile time
which is obviously true...


More information about the Digitalmars-d-learn mailing list