non empty slices

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 2 15:17:32 PDT 2016


On 06/02/2016 11:37 PM, Alex wrote:
> 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...

Yeah, can't do it that way. You have only one f_impl call, but want it 
to go to different overloads based on dynamic information (caseS). That 
doesn't work.

You need three different f_impl calls. You can generate them, so there's 
only one in the source, but it's a bit involved:

     sw: switch (caseS)
     {
         foreach (i, T; TL)
         {
             case i: f_impl(result.get!T); break sw;
         }
         default: assert(false);
     }



More information about the Digitalmars-d-learn mailing list