[IronPython] Array Access Problem
Jim Hugunin
jimhug at exchange.microsoft.com
Wed May 11 23:34:07 CEST 2005
Drew Moore wrote:
> Jim Hugunin wrote:
> >
> >Because of the way that value types work, there's no difference
between
> >the results of
> > apt[0].X = 0 and apt[0] = Point(0, apt[0].Y)
> >
> >The big question is how important it is that both of the following
are
> >equivalent.
> > apt[0].X = 0 and tmp = apt[0]; tmp.X = 0
> >
> Just curious, maybe I'm off base...
> Is concurrency a concern here?
The answer to this question is almost always yes no matter what the
context ;-)
> Can instances of value types be shared between threads? (Perhaps
> different languages?)
There aren't really instances of value types. That's part of the
problem. Value types are parts of other instances - either items in an
array or inlined fields in classes. The things that hold value types
can be shared between threads and languages.
> If so, what if another thread is concurrently modifying a different
> member of the underlying struct?
This is a good point. In that case there is a potential difference
between my two examples above. The one on the left can only change the
X part of the point, but the one on the right could in theory overwrite
another thread's changes to the Y part of the point.
> If the client code 'just changes one attribute', but in actuality the
> entire structure gets written back, it may make for some very
> hard-to-pin-down bugs.
> (if this might happen, maybe immutable value types aren't so bad.)
I don't see this as too much of a problem. If IronPython supports
apt[0].X=0, then that will only be implemented in such a way that just
the X part is modified. If we decided to require
apt[0]=Point(0,apt[0].Y) then it should be pretty clear to the
programmer that they're replacing both fields of the point
simultaneously.
I just tried this in J# to see if there was a clever idea to steal. J#
behaves just like the current IronPython does with value types:
Form f = new Form();
System.out.println(f.get_ClientSize());
f.get_ClientSize().set_Height(10);
System.out.println(f.get_ClientSize());
f.set_ClientSize(new Size(100, 100));
System.out.println(f.get_ClientSize());
{Width=292, Height=266} <-- starting size
{Width=292, Height=266} <-- height unchanged by set_Height
{Width=100, Height=100} <-- changed by setting to new Size object
-Jim
More information about the Ironpython-users
mailing list