Generating a method using a UDA

arturg var.spool.mail700 at gmail.com
Wed May 9 12:03:08 UTC 2018


On Wednesday, 9 May 2018 at 10:16:22 UTC, Melvin wrote:
> I'm trying to find a friendly syntax for defining things in a 
> framework. For context, I've been looking into finding a 
> solution for this problem 
> (https://github.com/GodotNativeTools/godot-d/issues/1) on the 
> Godot-D project. I've done some investigating already, and it 
> looks like I can only achieve what I want with a mixin, but I'd 
> like to get a second opinion.
>
> Say we have a class that defines a custom Signal (an event). In 
> an ideal world, the syntax would work similarly to this:
>
> class SomeNode : GodotScript!Node
> {
>     @Signal void testSignal(float a, long b);
>     // The declaration above would trigger the generation of 
> this line
>     void testSignal(float a, long b) { 
> owner.emitSignal("testSignal", a, b); }
>
>     @Method emitTest()
>     {
>         testSignal(3.1415, 42);
>     }
> }
>
>
> The reason I want to use a UDA is to stay consistent with the 
> other UDAs already defined for Properties and Methods. It also 
> looks friendlier than using a mixin. Does anyone here have any 
> thoughts as to how this could work?
>
> My main issue is injecting that generated method without 
> resorting to using a mixin. I was hoping that any code I needed 
> could be generated in the template that SomeNode inherits, but 
> that doesn't look possible because I can't inspect the subclass 
> (for good reason).

hi, i actually have something like that, which i should put on 
github.

i used it to learn about D's introspection, so its more of a 
prototype and will need some more work.

it looks like this:

class Test
{
     mixin signalsOf!SigList;

     interface SigList
     {
         @Signal void someFun(int);
     }

     void someFunHandler(int){}
}

signalsOf takes a type/template or function list, introspects 
them then generates the actual signal functions.
the additional api is similar to qt's api.

void main()
{
     Test t = new Test;
     t.connect!"someFun"(&t.someFunHandler);
     t.someFun(4); // emit the signal
     t.disconnect!"someFun"(&t.someFunHandler);
}

you can have different connection types and i also have string 
based connection and auto connection based on a naming convetion 
like signalname: someSig and slotname: onSomeSig.


More information about the Digitalmars-d-learn mailing list