jep icon indicating copy to clipboard operation
jep copied to clipboard

Operator overloading?

Open mramaiya opened this issue 2 years ago • 5 comments

So, I have a Java class that have the equivalent operator methods set, and I'm trying to then use the operators in Python. This works fine in python (if I wrote the class in python) and it works fine in Jython. If I call the add method directly, that works as well, but for some reason, if I do a + b, for some reason, it's unable to execute the operator method.

package cff.python.interpreter;

public class Signal {

    private final int n;

    public Signal() {
        this. n = 0;
    }

    public Signal(int n) {
        this. n = n;
    }

    public Signal __add__(int i) {
        return new Signal(this.n + i);
    }

    @Override
    public String toString() {
        return "Signal:" + n;
    }
}

package cff.python.interpreter;

import jep.Interpreter;
import jep.JepConfig;
import jep.SharedInterpreter;

public class Main {

    public static void main(String[] args) {
        SharedInterpreter.setConfig(new JepConfig().redirectStdout(System.out));
        try (Interpreter interpreter = new SharedInterpreter()) {
            interpreter.exec("""
                    from cff.python.interpreter import Signal;
                    s = Signal(1);
                    print(dir(s))
                    print(s.__add__)
                    print(s.__add__(4))
                    print(s + 1)                     
                    """);
        }
    }

}

I'm wondering if I'm missing something simple here.

mramaiya avatar Mar 25 '22 19:03 mramaiya

Unfortunately, this is not currently working on jep. For operator overload to work in general python needs the method objects to exist at the time of type creation but jep does not add them until object creation. I think it would be possible to change jep to add the method objects to the type dict before type creation but it would be a significant change. It is something I would like to add to jep at some point but I have not yet found the time to do so. There is some slightly related discussion about overriding repr on #339.

bsteffensmeier avatar Mar 25 '22 19:03 bsteffensmeier

Got it. Thanks for the quick response.

I looked through the code to see if I could implement it myself, but knowledge of C isn't strong enough to warrant it.

mramaiya avatar Mar 25 '22 20:03 mramaiya

I've rearranged the code so methods are added during type creation and added a test to verify that an __add__ method in java enables operator overload in python. The code is available in #386, if there are no problems with the change then I am hoping to get this capability in the next jep release.

@mramaiya If you have the ability to test this and make sure it works as expected it would be appreciated.

bsteffensmeier avatar Mar 27 '22 21:03 bsteffensmeier

Oh, man, that would be awesome. I really appreciate it.

Yeah, I’ll test it out. I’m guessing this would work for all the overloadable methods, right?

Milan

Sent from my hand On Mar 27, 2022, 4:21 PM -0500, Ben Steffensmeier @.***>, wrote:

I've rearranged the code so methods are added during type creation and added a test to verify that an add method in java enables operator overload in python. The code is available in #386, if there are no problems with the change then I am hoping to get this capability in the next jep release. @mramaiya If you have the ability to test this and make sure it works as expected it would be appreciated. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

mramaiya avatar Mar 27 '22 22:03 mramaiya

Yeah, I’ll test it out. I’m guessing this would work for all the overloadable methods, right?

Yes, in theory it should work for other methods, there is nothing specific to add in the change, it just adds the methods earlier so that python internal code can recognize them correctly.

bsteffensmeier avatar Mar 28 '22 16:03 bsteffensmeier

Closing as fixed in Jep 4.1.0.

ndjensen avatar Oct 31 '22 04:10 ndjensen