[IronPython] Writing cross-language libraries that appear native

Doug Blank doug.blank at gmail.com
Mon Apr 4 18:21:26 CEST 2011


On Mon, Apr 4, 2011 at 11:52 AM, Tomas Matousek
<Tomas.Matousek at microsoft.com> wrote:
> Re: 1.
>
> MutableString is convertible to String, so why would you need an object parameter?

Do you mean that one could write:

    public static string expects_string(MutableString val) {
        return (val as String);
    }

I wrote:

    public static string expects_string(object val) {
        return val.ToString();
    }

because that would work for all languages, and you could pass in an
IronPython or F# string it it would work just as well. Or did you mean
something else?

-Doug

> Tomas
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Doug Blank
> Sent: Monday, April 04, 2011 8:25 AM
> To: Discussion of IronPython
> Subject: [IronPython] Writing cross-language libraries that appear native
>
> I'm working on writing C# libraries which can be imported by a variety of .NET languages, DLR and otherwise, which appear native to the language importing them.
>
> For example, writing a C# function that can be used naturally in IronPython as if it were written in Python, IronRuby as if it were written in Ruby, and F# as if it were written in F#, etc.
>
> I've encountered some gotchas that I thought I'd share, and looking for any other points of advice in writing cross-language libraries.
>
> 1. IronRuby strings aren't really strings, so you need to pass them in as an object, and call .ToString().
>
>    public static string expects_string(object val) {
>        return val.ToString();
>    }
>
> 2. F# 2.0 doesn't seem to automatically convert a type to the associated nullable type, so avoid nullable types as parameters.
>
>    // AVOID:
>    public static double? expects_nullable(double? var1=null) {
>        return var1;
>    }
>
>    // BETTER:
>    public static double? expects_nullable() {
>        return null;
>    }
>    public static double expects_nullable(double var1) {
>        return var1;
>    }
>
> 3. IronPython and IronRuby lists and dictionaries implement IList and IDictionary, so no problem with those.
>
>    public static IDictionary<object,object> make_dict(object val) {
>        if (val as IDictionary<object,object> != null) {
>            return ((IDictionary<object,object>)val);
>        } else
>            throw new System.ArgumentException("object is not a dictionary");
>    }
>
>    public static IList<object> make_list(object val) {
>        if (val as IList<object> != null) {
>            return ((IList<object>)val);
>        } else
>            throw new System.ArgumentException("object is not a list");
>    }
>
> Any other suggestions?
>
> -Doug
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>



More information about the Ironpython-users mailing list