www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Run-time generic D implementation

reply =?UTF-8?B?Sm/Do28gTG91cmVuw6dv?= <jlourenco5691 gmail.com> writes:
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
Apr 28 2023
next sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
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 79
Apr 28 2023
parent =?UTF-8?B?Sm/Do28gTG91cmVuw6dv?= <jlourenco5691 gmail.com> writes:
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).
Apr 30 2023
prev sibling parent =?UTF-8?B?Sm/Do28gTG91cmVuw6dv?= <jlourenco5691 gmail.com> writes:
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.
May 07 2023