I'm guessing the collection you're adding it to isn't strongly typed?

The reason I ask is we will go through our conversion code paths which should 
do the right thing if it's not strongly typed.  One possible (horribly ugly) 
work around here is:

>>> import clr
>>> clr.AddReference('IronPythonTest')
>>> from IronPythonTest import Derived, Base
>>> from System.Collections.Generic import List
>>> x = List[Base]()
>>> y = Derived(7)
>>> x.Add(y)
>>> print x[0]
<Base object at 0x000000000000002C>
>>> print x[0].value
7

given the types:

    public class Base {
        public int value;

        public Base(int value) {
            this.value = value;
        }
    }

    public class Derived {
        public Derived(int value) {
            this.value = value;
        }

        public int value;

        public static implicit operator Base(Derived d) {
            return new Base(d.value);
        }
    }


So you could either use the generic list trick I used above or you could define 
a helper which takes in a double and returns a double (or a generic identity 
function which takes in a T and returns a T and then index into it with the 
type you want to use).

This is less than ideal.  In v2.0 we at least leave op_Implicit on the types so 
you can call it directly, but we probably want to enable calling the target 
type to do the conversion.  I've opened bug #11278 
(http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=11278) to 
track that we need to do a better job here, maybe we could leverage coerce.

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Michael Foord
Sent: Friday, June 22, 2007 7:52 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Implicit Cast to Float

Dino Viehland wrote:
> Is there only one implicit conversion?  If so I think you should be able to 
> call the op_Implicit method directly on XDate, e.g: XDate.op_Implicit(d).
>
> If there's more than one I'm not sure how you'd select the one you want to 
> call off the top of my head.
>

Unfortunately I've tried that already, and it appears not to exist:

 >>> import clr
 >>> clr.AddReference('ZedGraph')
 >>> from ZedGraph import XDate
 >>> d = XDate(1995, 1, 1)
 >>> XDate.op_Implicit(d)
Traceback (most recent call last):
AttributeError: type object 'XDate' has no attribute 'op_Implicit'
 >>> XDate.op_Implicit
Traceback (most recent call last):
AttributeError: type object 'XDate' has no attribute 'op_Implicit'

Michael
http://www.ironpython.info





> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Michael Foord
> Sent: Thursday, June 21, 2007 4:41 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] Implicit Cast to Float
>
> For the record - it *doesn't* implement IConvertible, so I can't use
> that. I've searched the list and can find a very old discussion of
> possible syntax - but no answer. :-(
>
> Michael
>
> Michael Foord wrote:
>
>> Hello all,
>>
>> I am trying to use a data type that must be 'cast to a float' - the
>> ZedGraph.XDate.
>>
>> It has what it calls an 'Implicit' operator:
>>
>> http://zedgraph.sourceforge.net/documentation/html/M_ZedGraph_XDate_op_Implicit_4.htm
>>
>> How can I get at this from IronPython? The obvious way doesn't seem to work:
>>
>>  >>> from ZedGraph import XDate
>>  >>> d = XDate(2007, 1, 1)
>>  >>> float(d)
>> Traceback (most recent call last):
>>   File , line 0, in <stdin>##25
>>   File , line 0, in Make##27
>> AttributeError: 'XDate' object has no attribute '__float__'
>>
>> I need to add this to a collection and it needs to be as a float.
>>
>> The C# syntax to do this is:
>>
>> x = (double) new XDate( 1995, 5, i+11 );
>>
>> Any help appreciated!
>>
>> Thanks
>>
>> Michael Foord
>> http://www.ironpython.info
>>
>> _______________________________________________
>> 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
> _______________________________________________
> 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
_______________________________________________
users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to