Is using function() in templates possible at all?
Sjoerd Nijboer
sjoerdnijboer at gmail.com
Wed Apr 11 21:07:03 UTC 2018
I am trying to do a binary insert into my sorted array.
To sort classes and structs I would like to give a delegate `(t)
=> t.myValue` to sort on that value whitout having to implement
an interface or specifically declare opCmp for every class I want
to have sorted.
After all, I might want one group of objects of a given class
sorted in one way and another group of objects sorted on another
way depending on the usecase.
In C# this is done by passing on a lambda in for instance a LinQ
expression to sort such list after insertion, and is also usefull
in other circumstances.
But is it possible in D to do something simular but then pass on
this Function() during compile time?
something like
`
void main() { SortedList!(Vector3, (v) => v.y) list; }
struct Vector3 { float x, y, z; }
class SortedList(T, int function(T) comparer)
{
T[] array;
int foo(T t)
{
for(int i = 0; i < array.lenght; i++)
{
if(comparer(this.otherT) <= comparer(t))
{
//do stuff
array[i] = t;
}
}
}
}
`
More information about the Digitalmars-d-learn
mailing list