non-global template fix POC WIP

Jonathan Marler johnnymarler at gmail.com
Sun Jan 27 18:31:37 UTC 2019


On Sunday, 27 January 2019 at 18:13:49 UTC, Jonathan Marler wrote:
> On Saturday, 26 January 2019 at 12:06:48 UTC, Suleyman wrote:
>> I think it's ready for use. I would appreciate it if someone 
>> can break it and produce a test case.
>
> I downloaded your branch and created this example that doesn't 
> work:
> ---------------------------------------------------------
>
> import core.stdc.stdio : printf;
>
> struct Adder
> {
>     int base;
>     // works
>     uint addBaseCallFunc(alias func)(uint value)
>     {
>         return func(base + value);
>     }
>
>     // these 2 don't work
>     uint willNeedThreeContextPtrs(alias func)(uint value)
>     {
>         int localVar = func(value);
>         uint localFunc(uint subValue) { return localVar + 
> subValue; }
>         return addBaseCallFuncs!(func, localFunc)(value);
>     }
>     uint addBaseCallFuncs(alias func1, alias func2)(uint value)
>     {
>         return func2(func1(base + value));
>     }
> }
>
> void main()
> {
>     auto adder = Adder(3);
>
>     int offset = 6;
>     uint doOffset(uint value) { return offset + value; }
>
>     // works
>     printf("%d\n", adder.addBaseCallFunc!doOffset(4));
>
>     // need 3 context pointers (doesn't work)
>     printf("%d\n", adder.willNeedThreeContextPtrs!doOffset(4));
> }

Here's another one that seems to compile, but fails at runtime 
(it doesn't print anything):

import core.stdc.stdio : printf;

struct Adder
{
     int base;
     uint addBaseCallFuncs(alias func1, alias func2)(uint value)
     {
         return func2(func1(base + value));
     }
}

void main()
{
     auto adder = Adder(3);

     int offset = 6;
     uint doOffset1(uint value) { return offset + value; }

     void anotherFunc()
     {
         int anotherVar = 11;
         uint doOffset2(uint value) { return anotherVar + value; }
         printf("%d\n", adder.addBaseCallFuncs!(doOffset1, 
doOffset2)(4));
     }
}



More information about the Digitalmars-d mailing list