[IronPython] Emulating numeric type in C# or VB.net , __radd__ is not called

robsp rsaat at usa.com
Tue Jun 9 06:03:06 CEST 2009


Thank you for your help. 

I tested only __add__ and __radd__ with Ironpython 2.0

The method __add__ could be implemented using the following functions using
VB Syntax: 

 Public Shared Function __add__(ByVal Self As MyNumericObject, ByVal other
As Object) As MyNumericObject

 Public Function __add__(ByVal other As Object) As MyNumericObject ' not
static 

 Public Shared Operator +(ByVal Self As MyNumericObject, ByVal other As
Object) As MyNumericObject

The method __radd__ could be implemented using the following functions using
VB Syntax: 

Public Shared Operator +(ByVal other As Object, ByVal Self As
MyNumericObject) As MyNumericObject

Public Shared Function __radd__(ByVal other As Object, ByVal Self As
MyNumericObject) As MyNumericObject

The non static method does not work for __radd__. 



David DiCato wrote:
> 
> Hmm, the fact that we aren't picking up __radd__ properly looks like a
> bug, which I'll go ahead and log on CodePlex. The good news is that
> there's a more common usage pattern that works correctly.
> 
> The canonical way to do this is to define + and - as static (Shared in VB
> speak) operator methods. These should automatically manifest themselves as
> __add__, __radd__, __sub__, and __rsub__ in IronPython. Specifically, I
> recommend trying something like this:
> 
> Public Structure NumericObject
>                 ...
> 
>                 ' This method should become __add__
> Public Shared Operator +(ByVal self As NumericObject, ByVal other As
> Object) _
>                 ...
> End Operator
> 
> ' This method should become __radd__
> Public Shared Operator +(ByVal other As Object, ByVal self As
> NumericObject) _
>                 Return self + other
> End Operator
> 
> ' Ditto with subtraction
> ...
> End Structure
> 
> In addition, if you're only interested in adding/subtracting objects of
> the same type, you need only define each operator once:
> 
> Public Structure NumericObject
>                 ...
> 
>                 ' This method should become __add__ and __radd__
> Public Shared Operator +(ByVal self As NumericObject, ByVal other As
> NumericObject) _
>                 ...
> End Operator
> 
> ' Ditto with subtraction
> ...
> End Structure
> 
> I've only tested this in C# against IPy 2.6, so please let me know how it
> turns out. Good luck!
> - David
> 
> From: users-bounces at lists.ironpython.com
> [mailto:users-bounces at lists.ironpython.com] On Behalf Of Ricardo
> Sent: Sunday, June 07, 2009 7:26 PM
> To: users at lists.ironpython.com
> Subject: [IronPython] Emulating numeric type in C# or VB.net , __radd__ is
> not called
> 
> I testing  IronPython 2.0  and trying to integrate with my VB.NET
> application.
> 
> I would like to create an object to emulate a numeric type implementing
> the functions
> __add__, __radd__ , __sub__ , __rsub__ , etc as explained in Python manual
> 
> http://www.python.org/doc/2.5.2/ref/numeric-types.html
> 
> I would like to create this emulated object inside my vb.net application
> and import this object from a python code like the example below
> 
> #IronPython code
> 
> #Vb.NET object
> from IronPytonTest import TestNumericObject
> 
> # My numeric object with value 1
> test=TestNumericObject(1)
> 
> print float(test+1) #print 2 ( 1 + 1 ) OK , It´s called the function
> __add__  from my vb code
> 
> print float(test+test) #print 2 ( test + test = 1+1 ) OK , It´s called the
> function __add__  from my vb code
> 
> print float(1+test)
> # Error: Microsoft.Scripting.ArgumentTypeException: unsupported operand
> type(s) for +: 'int' and 'TestNumericObject'
> #__radd__ function is not called
> 
> Below is my vb code , basically I implemented the functions  __radd__, 
> __add__   in my vb.net code and trying to sum my objects inside python,
> but __radd__ is not called.
> 
> Public Class TestNumericObject
> 
>     Sub New(ByVal value As Double)
>         m_value = value
>     End Sub
> 
>     Private m_value As Double
>     Public ReadOnly Property Value() As Double
>         Get
>             Return m_value
>         End Get
>     End Property
> 
>     Public Function __float__() As Double
>         Return m_value
>     End Function
> 
>     Public Function __add__(ByVal other As Object) As TestNumericObject
>         Dim v As Double
>         If TypeOf other Is TestNumericObject Then
>             v = CType(other, TestNumericObject).m_value
>         Else
>             v = CDbl(other)
>         End If
>         Dim o As New TestNumericObject(0)
>         o.m_value = Me.m_value + v
>         Return o
>     End Function
> 
>     Public Function __radd__(ByVal other As Object) As TestNumericObject
>         Dim v As Double
>         If TypeOf other Is TestNumericObject Then
>             v = CType(other, TestNumericObject).m_value
>         Else
>             v = CDbl(other)
>         End If
>         Dim o As New TestNumericObject(0)
>         o.m_value = Me.m_value + v
>         Return o
>     End Function
> 
> End Class
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> 
> 

-- 
View this message in context: http://www.nabble.com/Emulating-numeric-type-in--C--or-VB.net-%2C-__radd__-is-not-called-tp23917438p23936019.html
Sent from the IronPython mailing list archive at Nabble.com.




More information about the Ironpython-users mailing list