CTFE Status 2

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Fri May 12 04:21:56 PDT 2017


On Thursday, 16 February 2017 at 21:05:51 UTC, Stefan Koch wrote:
> [ ... ]

Hi Guys,

Outer function arguments are now supperted.

meaning this code will now work:

int[] filterBy(int[] arr , bool function(uint) fn)
{
     int[] result = [];
     uint resultLength;

     result.length = arr.length;
     foreach(i;0 .. arr.length)
     {
         auto e = arr[i];
         bool r = true;
         r = fn(e);
         if(r)
         {
             result[resultLength++] = e;
         }
     }

    int[] filterResult;
    filterResult.length = resultLength;

    foreach(i; 0 .. resultLength)
    {
      filterResult[i] = result[i];
    }

   return filterResult;
}

bool isDiv2(uint e)
{
   bool result;
   result = (e % 2 == 0);
   return result;
}


static assert(filterBy([3,4,5], &isDiv2) == [4]);

before this would behaved very strangely ;)
because isDiv would have been executed instead filterBy.
And since after bytecode compilation there is no type checking 
anymore the arrayPtr would have been interpreter as an integer.
(which is always 4byte aligend)
that would have caused the isDiv to return 1;
which would have been interpreted as address.
and whatever was there would have been treated as array 
descriptor.
resulting in mostly the [] return value.

...
anyway.
I am happy this is fixed now.


More information about the Digitalmars-d mailing list