Run-time generic D implementation

Salih Dincer salihdb at hotmail.com
Sat Apr 29 00:21:33 UTC 2023


On Friday, 28 April 2023 at 20:04:55 UTC, João Lourenço wrote:
> Hey, I've been working on a run-time generic concept for D. I 
> really like the way Zig approaches allocators in their language 
> and this was inspired by it. This is just a POC, not a complete 
> project, but feel free to analyze it and play with it!
>
> https://gist.github.com/iK4tsu/5e736365e5066a02ca372fd79ec33ce9

So, how is it different from the code below? I can understand 
though I leave the class under the control of the GC using the 
new operator. But you used struct...

```d
void main()
{
   interface Foo
   {
     int fun() @safe pure nothrow @nogc;
     int fun(int) @safe pure nothrow @nogc;
   }

   class MyFooDouble : Foo
   {
     int fun() => 5;
     int fun(int i) => i * 2;
   }

   class MyFooTriple : Foo
   {
     int fun() => 5;
     int fun(int i) => i * 3;
   }

   Foo myFooDouble = new MyFooDouble;
   Foo myFooTriple = new MyFooTriple;

   //alias genericFoo = genericOf!Foo;

   auto foos = imported!"std.array".staticArray([
     myFooDouble, myFooTriple/*
     genericFoo(() @trusted { return &myFooDouble; } ()),
     genericFoo(() @trusted { return &myFooTriple; } ()),//*/
   ]);

   with(foos[0]) assert(fun(fun) == 10);
   with(foos[1]) assert(fun(fun) == 15);
}
```

SDB at 79


More information about the Digitalmars-d mailing list