www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - problem with array of delegates!

reply "Lloyd Dupont" <ld-REMOVE galador.net> writes:
the following code seem problematic to compile...

====
import std.algorithm;
private alias void delegate(int, int) SlotDelegate;

class A
{
    void DIT(int a, int b)
    {
    }
}

int main(string[] argv)
{
    A a;
    SlotDelegate x= &a.DIT;

    SlotDelegate[] _slotDg;
    _slotDg.remove(x);

    return 0;
}
====
I have some strange error:
Error: incompatible types for ((pos) < (from)): 'uint' and 'void 
delegate(int, int)' 
C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d    5609


I try a method like that as well (but similar problem! :( )
====
void removeAt(T)(ref T[] array, int index)
{
    if(index < 0 || index >= array.length)
        return;
    const T[] empty = null;
    array.replaceInPlace(index, index + 1, empty);
}
==== 
Jun 19 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Remove takes an offset, not a value as far as I know.

If you need fast lookup and removal you could use hashes instead:

int main(string[] argv)
{
    auto a = new A;
    SlotDelegate x = &a.DIT;

    bool[SlotDelegate] _slotDg;
    _slotDg.remove(x);

    return 0;
}
Jun 19 2011
parent "Lloyd Dupont" <ld galador.net> writes:
There is a remove() method in std.algorithm!I even got asked why I was 
reimplementing it!
(well, because I didn't know it existed hey!)

works fine with, say, int...

but not with delegate!

associative array will solve the problem indeed.. (I hope) but they use way 
more memory!
it would be nice to have remove working() :)


Further, as you can see in my post, even my (reasonable) implementation of 
removeAt() fail! :(
(but, again, it works for int!)




"Andrej Mitrovic"  wrote in message 
news:mailman.1010.1308495216.14074.digitalmars-d-learn puremagic.com...

Remove takes an offset, not a value as far as I know.

If you need fast lookup and removal you could use hashes instead:

int main(string[] argv)
{
    auto a = new A;
    SlotDelegate x = &a.DIT;

    bool[SlotDelegate] _slotDg;
    _slotDg.remove(x);

    return 0;
} 
Jun 19 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Seems like gmail likes to cut my code. If that didn't paste well here:
http://codepad.org/cyEDHSGc
Jun 19 2011