[IronPython] Why would you use MakeList over MakeListNoCopy?

Dino Viehland dinov at microsoft.com
Sat Oct 11 23:01:34 CEST 2008


Yep, that's fine - C# creates a new array on every params call.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dan Eloff
Sent: Saturday, October 11, 2008 2:00 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Why would you use MakeList over MakeListNoCopy?

On Sat, Oct 11, 2008 at 3:38 PM, Dino Viehland <dinov at microsoft.com> wrote:
> The difference is whether or not you're the owner of the array.  If you've accepted the array from some public location, even if it was a params method, someone else could own the array and continue to modify it.  If you've created the array yourself or can guarantee it won't change then it can safely be used for the underlying object array for the List object.

That's what I figured about 5 minutes after I sent the email :( This
stuff is burning my brain. One thing that is unclear to me, if you do
l = MakeListNoCopy(x, y), is it legal to do l[0] = z ? It seems a
little shaky to me, but if mutating a params array is legal in C#,
then I suppose it shouldn't break.

Thanks,

-Dan


> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Dan Eloff
> Sent: Saturday, October 11, 2008 1:29 PM
> To: Discussion of IronPython
> Subject: [IronPython] Why would you use MakeList over MakeListNoCopy?
>
>        /// <summary>
>        /// Python runtime helper to create a populated instance of
> Python List object.
>        /// </summary>
>        public static List MakeList(params object[] items) {
>            return new List(items);
>        }
>
>        /// <summary>
>        /// Python runtime helper to create a populated instance of
> Python List object w/o
>        /// copying the array contents.
>        /// </summary>
>        [NoSideEffects]
>        public static List MakeListNoCopy(params object[] items) {
>            return List.FromArrayNoCopy(items);
>        }
>
> If the second method "just works" then why would you ever want to use MakeList?
>
> I seem to be creating a lot of 1 and 2 item lists here, so I'm looking
> to make good use of one of these guys. (I really miss C# 3.0
> collection initializers)
>
> Work on porting _ast is going well, so far I've had no insurmountable
> obstacles. I expect to be done it by Monday. The AST produced is close
> (but not identical) to CPython, but that shouldn't matter. Each node
> has the same properties with the exact same types so walking the tree
> should work equally on both platforms.
>
> Thanks,
> -Dan
> _______________________________________________
> 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
>
_______________________________________________
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