There's no general way to be ensure the RHS will be invoked for a binding in particular due to the issues you've come up with.
It's also problematic for IronPython to ask the RHS to do the binding when the LHS is not an IDMOP because the operations may not be symmetric. Really we'll need to add some new operations to the DLR to support this (e.g. a BindReverseBinaryOperation or something along those lines). But we didn't get to that for DLR v1 - maybe we can do it for the next version and we can add some interfaces to the DLR outer layer to try it out in the mean time. > -----Original Message----- > From: [email protected] [mailto:users- > [email protected]] On Behalf Of Jeffrey Sax > Sent: Friday, February 19, 2010 9:17 AM > To: 'Discussion of IronPython' > Subject: [IronPython] Asymmetry in binary binding > > 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 > > _______________________________________________ > Users mailing list > [email protected] > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com _______________________________________________ Users mailing list [email protected] http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
