-
1. Re: Getters that return a reference to an Array
Claudiu Ursica Nov 29, 2010 2:02 AM (in response to hidarikani)Actually a getter does not allow you to modify anything by itself only to read
that piece of data. If you want to modify you need the public setter also. You
need to clone only if you want some undo functionality e.g. flex passes objects
through reference so if you modifiy and want to revert is better to have a copy
of the data and not the reference to the data.
C
-
2. Re: Getters that return a reference to an Array
GordonSmith Nov 29, 2010 1:16 PM (in response to Claudiu Ursica)A getter that returns an array allows callers to get the array and modify it, with impact on future callers. If you want to make that impossible, the getter should return a clone of the array, but this will be more inefficient.
Gordon Smith
Adobe Flex SDK Team
-
3. Re: Getters that return a reference to an Array
Claudiu Ursica Nov 29, 2010 1:35 PM (in response to GordonSmith)Can you post a simple example please, obviously I got this wrong, and not sure I
follow you.
10x,
C
-
4. Re: Getters that return a reference to an Array
GordonSmith Nov 29, 2010 3:43 PM (in response to Claudiu Ursica)Suppose one method gets the array and modifies its first element:
private function f1()
{
myArr[0] = "abc";
}
Then when another method gets the array, the first element will have the new value, because it is getting the same array rather than a clone:
private function f2()
{
trace(myArr[0]); // "abc"
}
In addition to changing existing elements, callers could also insert or remove elements, changing the length. These kinds of changes could perhaps have bad side effects, so sometimes people like to return clones. Other people just document on the getter something like "Don't change the array that is returned by this getter".
Gordon Smith
Adobe Flex SDK Team
-
5. Re: Getters that return a reference to an Array
Claudiu Ursica Nov 30, 2010 1:43 AM (in response to GordonSmith)Got it thanks,
I was cloning objects before I think even collections but never thought of the
array like this.
Cheers,
Claudiu
-
6. Re: Getters that return a reference to an Array
hidarikani Nov 30, 2010 11:12 PM (in response to Claudiu Ursica)By the way ArrayCollection.toArray() clones the internal Array.



