[IronPython] Asymmetry in binary binding

Jeffrey Sax jeffrey at extremeoptimization.com
Fri Feb 19 18:17:18 CET 2010


Hi,

I have the following C# code:

public class MyObject : IDynamicMetaObjectProvider { 
    int value;
    public MyObject(int value) { this.value = value; }
    static public int operator *(int left, MyObject right) { return left *
right.value; }
    static public int operator *(MyObject left, int right) { return
left.value * right; }
    DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression
parameter) {
        return new MyMetaObject(parameter, this);
    }

    class MyMetaObject : DynamicMetaObject {
        public MyMetaObject(Expression parameter, MyObject value)
        : base(parameter, BindingRestrictions.Empty, value) { }
        public override DynamicMetaObject BindBinaryOperation(
                BinaryOperationBinder binder, DynamicMetaObject arg) {
            Console.WriteLine("Binding: {0}", binder.Operation.ToString());
            return base.BindBinaryOperation(binder, arg);
        }
    }
}

In IPy:
>>> c = MyObject(10)
>>> 5*c
50
>>> c*5
Binding: Multiply
50

So, MyObject's custom binding logic is only invoked if it is the left
operand of a binary operation. This asymmetric behavior doesn't seem right:
I expected MyMetaObject's binder to be called in both cases.

I realize that this would have its own issues. For example: which bind
method has precedence if the left and right operands both implement IDMOP?
(You could come up with a mechanism similar to method call overload
resolution.)

Is there a way with the current DLR to still get MyObject's binder to be
invoked if it's the right operand?

Thanks,
Jeffrey




More information about the Ironpython-users mailing list