CTFE Status

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Fri Feb 3 10:54:08 PST 2017


On Wednesday, 1 February 2017 at 11:53:09 UTC, Stefan Koch wrote:
> On Tuesday, 31 January 2017 at 16:21:27 UTC, Stefan Koch wrote:
>> function pointer support is coming!
>
> This is more difficult then I expected,
> since the argument handling system was build without 
> considering that we could have functions as arguments, 
> therefore when trying to evaluate the function-argument it will 
> overwrite the data-structures that are supposed to hold the 
> information for the function we currently processing the 
> arguments for.
>
> maybe this can be fixed by handling the arguments before 
> handling the function itself.

I have not fixed the bug with top-level function arguments yet,
However I have enabled function-pointer support for non-toplevel 
use
such as in :
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_;
}

bool isNotDiv2(uint e)
{
   bool result_;
   result_ = (e % 2 != 0);
   return result_;
}

int[] run(int[] arr, bool div2)
{
   return filterBy(arr, div2 ? &isDiv2 : &isNotDiv2);
}


static assert(run([3,4,5], true) == [4]);
static assert(run([3,4,5], false) == [3,5]);

//static assert(filterBy([3,4,5], &isDiv2) == [4]);
// top-level function arguments do currently not work with newCTFE

Enjoy!


More information about the Digitalmars-d mailing list