Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement higher-level API on abstract #8

Open
nadako opened this issue Feb 23, 2019 · 2 comments
Open

implement higher-level API on abstract #8

nadako opened this issue Feb 23, 2019 · 2 comments

Comments

@nadako
Copy link
Member

nadako commented Feb 23, 2019

it should be possible with Haxe to implement a very nice OO-ish API on top of the basic defold api with zero run-time cost using abstracts. look into that.

@nadako
Copy link
Member Author

nadako commented Mar 5, 2019

another idea would be to provide macro-powered sugar for handling and sending messages (see https://forum.defold.com/t/haxe-defold/44368/8)

that begs for property sugar as well, ok now I want to design something :)

@haath
Copy link
Member

haath commented May 29, 2020

Would this API be ok?

import defold.support.CallbackDispatch;

override function on_message<T>(self:Props, message_id:Message<T>, message:T, sender:Url)
{
    // Macro function to generate callback calls.
    CallbackDispatch.dispatchMessage(message_id, message, sender);
}

Then the callbacks could look like this:

typedef MessageOne = {
    var param1: Int;
    var param2: Vector3;
}
typefdef MessageTwo = { ... }

@:message function onMessageOne(self:Props, sender:Url, msg:MessageOne) { }
@:message function onMessage2(self:Props, sender:Url, msg:MessageTwo) { }

The message type here is deducted from the third parameter.

Another alternative would be to do it like this.

@:message(msg:MessageOne) function onMessageOne(self:Props, sender:Url, param1:Int, param2:Vector3) { }

A bit prettier, but would cause compilation errors when the message typedef is updated, all callback methods using said message would need to be updated manually.


Then I'm also thinking we could have generated public static methods for posting messages.

public static inline function postMessageOne(url: Url, param1: Int, param2: Vector3) {
    // The message id will of course be pre-hashed somewhere.
    Msg.pos(url, hash("_TypeName_MessageOne_",
    {
        param1: param1,
        param2: param2
    })
}

Or is it better to not have statics at all, and go straight for an abstract OOP implementation?
I have sort of started doing something similar here.


Ditto all of the above for on_input as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants