I implemented delegates in D
maik klein via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jun 9 14:02:26 PDT 2016
I am currently writing a task system and I have this case where I
want to send a delegate to a different thread but this delegate
also captures a variable. I use this to implement a "future".
Now as far as I know this delegate will allocate GC memory and I
just wanted to avoid that, just for fun.
Here is the code https://dpaste.dzfl.pl/cd77fce99a5b
I have only worked on it a couple of hours and I am sure there
are many problems with it.
Basically the idea is that you can use normal lambda syntax. If
you want a function that returns and int and takes an int, you
can write it like this:
(int i) => i
If you want a function that returns an int, takes an int, but
also captures and int you would write it like this
(int i, int captured) => i + captured
But you also have to declare the base function type without the
captured variables beforehand.
Fn!(FnType!(int, int), (int i, int captured) => i + captured)(42);
That is how I know what the captured variables are.
The only part that is currently missing are polymorphic
delegates. They are not too useful if I can't pack them into the
same array.
I guess I have to do this with classes/interfaces.
Thoughts?
Has this been done before?
More information about the Digitalmars-d
mailing list