rete icon indicating copy to clipboard operation
rete copied to clipboard

Socket compatibility should be determined by Input socket not Output socket

Open thomaswp opened this issue 4 years ago • 1 comments

Currently, socket compatibility is determined by calling (in rete/output.ts):

        if (!this.socket.compatibleWith(input.socket))
            throw new Error('Sockets not compatible');

The output node determines whether it is compatible with the input node. I believe it should be the other way around, with the input node determining whether it can accept a given output.

Simple example:

It would be intuitive to say, a float (input) is compatible with an int (output), but an int is not compatible with a float. However, if I specified this in my sockets, I would actually get the opposite behavior than expected.

More complex example:

You could say the above is just an example of semantics, that I could just swap which socket is compatible with which and it will work. However, for my current situation, I can't use that solution. I'd like to create an "Any input" socket. I thought this would be as simple as overriding the compatibleWith method to return true. But of course this does not work because the output checks compatibility, not the input. I could of course update all sockets I have (past and future) to be compatible with my any input socket, but that seems overwrought.

My ultimate goal is to create components with generic inputs and outputs (e.g. an "add" block that can work with integers or floats, and updates its output socket accordingly). Again, an input-centric model of compatibility makes much more sense here.

Is there any reason the output socket checks compatibility rather than the input? If not, could you swap it?

thomaswp avatar Feb 03 '22 14:02 thomaswp

P.S. a quick-fix for anyone with a similar goal in the meantime -- Have your sockets inherit from a base class:

class BaseSocket extends Socket {
    compatibleWith(socket, noReverse) {
        if (noReverse) return super.compatibleWith(socket);
        // Flip this to have input check compatibility
        return socket.compatibleWith(this, true);
    }
}

thomaswp avatar Feb 03 '22 14:02 thomaswp

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days.

rete-js[bot] avatar Jul 13 '23 10:07 rete-js[bot]