adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
531 stars 128 forks source link

Simpledisplay - empty handler doesn't work #338

Open helikopterodaktyl opened 2 years ago

helikopterodaktyl commented 2 years ago

What determines what is usable as a handler and what isn't? I am getting inconsistent behavior

import arsd.simpledisplay;
import std.stdio;

void main()
{
    SimpleWindow window = new SimpleWindow(Size(1024, 768), "Test");

    void foo()
    {
    }

    // works
    window.eventLoop(1,
        () { foo(); }
    );

    // doesn't work
    window.eventLoop(1,
        () { writeln("test"); }
    );

    // doesn't work
    window.eventLoop(1,
        () {}
    );

}
adamdruppe commented 2 years ago

They need to be delegates instead of function pointers. That's why the error message in that case suggests trying the delegate keyword - it specifies to the compiler that you need that specifically (though it is true the library could convert them internally too...).

The difference between them is a delegate accesses some local data and a function pointer doesn't. So { foo(); } works because foo is the local data it accesses. writeln doesn't because it is global, not local. And empty {} needs no extra data at all. So the compiler types them as function pointer.

If you use the delegate keyword though, the compiler just passes it a null pointer to local data, which since it doesn't use anyway, all works nicely.

Since you can just add the keyword on the outside when needed, I think that's good enough but it is tempting to make the library just go ahead and convert when necessary too, it wouldn't be that hard to add.