Thread overview
Run-time generic D implementation
Apr 28, 2023
João Lourenço
Apr 29, 2023
Salih Dincer
Apr 30, 2023
João Lourenço
May 07, 2023
João Lourenço
April 28, 2023

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

April 29, 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...

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@79

April 30, 2023

On Saturday, 29 April 2023 at 00:21:33 UTC, Salih Dincer wrote:

>

On Friday, 28 April 2023 at 20:04:55 UTC, João Lourenço wrote:

>

[...]

The difference is precisely that. Yes, using classes will achieve the same in practice, but you restrict the usage. I wanted something that did not depend on GC and could be used with betterC. Interestingly enough you can use interface with betterC as long as you don't instantiate any. Although this currently does not work with betterC due to std.format usage (and maybe other Phobos' methods).

May 07, 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

The code now compiles with both DMD and LDC and works in betterC.