For the list case, I need the count - it would be nice to get it fast
:) but I can work without it.

For the dict case, will that handle __missing__? I thought I tried it
and it didn't work, but I could have missed something.

- Jeff

Sent from my Windows® phone.

-----Original Message-----
From: Dino Viehland <di...@microsoft.com>
Sent: Friday, April 30, 2010 5:42 PM
To: Discussion of IronPython <users@lists.ironpython.com>
Subject: Re: [IronPython] Dealing with custom sequence/mapping types from C#


Jeff wrote:
> Given the following types (from sqlite3's tests):
>
>     class L(object):
>         def __len__(self):
>             return 1
>         def __getitem__(self, x):
>             assert x == 0
>             return "foo"
>
>     class D(dict):
>          def __missing__(self, key):
>              return "foo"
>
> Instances of these get passed in to a function implemented in C# and I
> need to able to treat them the same as built-in types. The built-in
> types happily implement either IList or IDictionary, so a cast will
> suffice, but that doesn't work for custom objects or subclasses like
> the above.
>
> Is there anything built-in that can handle this? I trawled through
> PythonOps looking for something but didn't find it. Something similar
> to PythonOps.CreatePythonEnumerable that returns IList/IDictionary and
> uses sequence/dictionary behaviour, I guess.

Do you absolutely need indexing or can you get by w/ enumeration?  Obviously
with enumeration you could ultimately get indexing but it'll be slower :)

My suggestion is to implement overloads like this:

    public static void X(IEnumerable foo) {
        Console.WriteLine(foo);
    }

    public static void X(IDictionary foo) {
        Console.WriteLine(foo);
    }



The L instance will be wrapped into a IronPython.Runtime.ItemEnumerable
object because it defines __getitem__.  The D instance will select the
IDictionary overload and you'll have a stronger API there.

In general you want to keep the C# code as statically typed as possible
(instead of taking an object) and let the overload resolver/conversion
logic kick in and do its thing.  The nice thing about that is that we'll
get all of our call site caching going on and things will be nice and
optimal.  If necessary we could look at adding a conversion of user
defined objects to IList as well but I don't think that one exists
today.
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to