Append(aa as Object)
Append an AssociativeArray to this one.
It is unclear what happens if the same keys are present in both dictionaries? E.g. a={k:“a”} and b={k:“b”}, after a.append(b), should that be:
a) error during update
b) a.k = “a” (a prevails)
c) a.k = “b” (b prevails)
d) behavior undefined (can change in the future)
I tested and current behavior seems to be (c), as if a loop over b has been ran, doing a.AddReplace(). Roku*, can you state if this will be the behavior to rely on and document it?
PS. method probably should have been named “update()” since this implies new values win, compare to python’s
PPS. there is a bug in “.append()” in that if one passes non-roAssociativeArray argument, nothing happens (not even an error). That does not sound meaningful by design. Currently by the doc it is “Append(aa as Object)” but i can feed it number, string, bool, function… with no apparent effect. So seems to be more of “dynamic” type
“RokuJoel” wrote:
Hi Enterr, I filed a bug on the last part of your posting (the lack of type checking) the day after you initially posted it.
Documentation - I can point that out to the documentation people.
Thanks Joel -
bump was regarding answer to my question. I guess the implication of what you are saying is that the new values are supposed to override old values for overlapping keys?
By the way, here is another one for reporting: roArray.append() acts the same way as append for roAA. For example, pop-quiz: what does this do:
a = [1,2,3]
a.append(4)
print a
a) print 1 2 3 4
b) error message
People coming from other languages (like python) will guess (a). The ones who have read the documentation and programmed only a little (like me) will expect (b). But the reality right now is
(c) none of the above, it will print 1 2 3
Hi, as you noted “b” prevails. That would be the standard behavior that I don’t think we would want to change. Of course, its software, so everything can change if someone decides to make it so as a matter of policy, or accidentally breaks something…
“RokuJoel” wrote:
Hi, as you noted “b” prevails. That would be the standard behavior that I don’t think we would want to change. Of course, its software, so everything can change if someone decides to make it so as a matter of policy, or accidentally breaks something…
Hi - appreciate an answer but since you don’t seem certain, please run this by “a higher power” and get definitive answer that it will not in fact change.
It is customary to fix such details in the API and document that too. As i pointed out this is the guaranteed behavior in other languages (although there they use more fortunate name like “.update()” which just by name implies the behavior).
I will give you a reason so my question does not seem capricious: i intend to use this for (home-grown, class-less) object inheritance, so that new methods override parent’s existing methods.
“EnTerr” wrote:
I will give you a reason so my question does not seem capricious: i intend to use this for (home-grown, class-less) object inheritance, so that new methods override parent’s existing methods.
I’m already using it like this as a fundamental feature of my framework, so I’m counting on it not changing! Looping through the keys individually is significantly slower (which, incidentally, can have a huge impact on the framerate of 2D API implementations).
There will never be a definitive answer that something will not change. Anything that is likely to break channels, the way changing this would break channels, is unlikely to change, but that doesn’t mean it won’t change if there is a pressing reason to do so.
I notice that documentation has been updated to say
“ifAssociativeArray.Append(aa as Object)” wrote:
… If any key in aa are already associated with a value in this AssociativeArray, the current value is discarded and is replaced with the value in aa.So it is behavior (B), the new AA overrides old AA content in case of overlapping keys. As any experienced developer would expect but now is in the RTFM and can be relied upon.
My gratitude goes to the forum’s patron saint, RokuMarkn - thank you!
“EnTerr” wrote:
I notice that documentation has been updated to say
“ifAssociativeArray.Append(aa as Object)” wrote:
… If any key in aa are already associated with a value in this AssociativeArray, the current value is discarded and is replaced with the value in aa.So it is behavior (B), the new AA overrides old AA content in case of overlapping keys. As any experienced developer would expect but now is in the RTFM and can be relied upon.
My gratitude goes to the forum’s patron saint, RokuMarkn - thank you!
It’s probably worth noting that case matters in this instance as well. So, if you have a key named “Test” and you append an AA with a key named “test”, you’ll end up with two separate keys.
“TheEndless” wrote:
It’s probably worth noting that case matters in this instance as well. So, if you have a key named “Test” and you append an AA with a key named “test”, you’ll end up with two separate keys.
That’s interesting. Could that be considered a bug? i thought Associative Arrays were case insensitive by default, hence the SetModeCaseSensitive interface. Which one do you get if you Lookup “test”? How about “tESt”?
“renojim” wrote:
That’s interesting. Could that be considered a bug?
I would consider this a bug - if it were true. But i am unable to reproduce the behavior:
BrightScript Debugger> d = {}
BrightScript Debugger> d["Test"] = "Test"
BrightScript Debugger> ?d
Test: Test
BrightScript Debugger> d2 = {}
BrightScript Debugger> d2["test"] = "test"
BrightScript Debugger> ? d2
test: test
BrightScript Debugger> d.append(d2)
BrightScript Debugger> ?d
test: test
It’s probably worth noting that case matters in this instance as well. So, if you have a key named “Test” and you append an AA with a key named “test”, you’ll end up with two separate keys.
So according to EnTerr’s test it seems this is not the case unless you call setModeCaseSensitve? The key replaced the other.
Brightscript is case-insensitive except for the registry, right? Anything else?
Sorry, it seems case sensitivity is on when parsing a JSON string, so this is only true if you call the SetModeCaseSensitive() explicitly, or if your AA comes from a parsed JSON string.
Brightscript is case-insensitive except for the registry, right? Anything else?
From the Brightscript Language Reference:
The dot operator is always case insensitive. By convention, a statement like:
aa.NewKey = 55
Will actually create the Associative Array entry in all lower case (“newkey”). The array operator or the ifAssociativeArray.AddReplace method can be used to create mixed case keys.
“TheEndless” wrote:
Sorry, it seems case sensitivity is on when parsing a JSON string, so this is only true if you call the SetModeCaseSensitive() explicitly, or if your AA comes from a parsed JSON string.
Thank you for the heads-up. I see the documentation mentions explicitly (yes! i am glad) the roAAs returned by ParseJSON will be in case-sensitive mode. It occurred to me a way to circumvent that as issue would be:
It was a guess and i tested it - turns out .append() honors the case-sensitivity mode flag of the host AA, so just pouring from a case-sensitive into an insensitive AA “fixes” our issue. IMO things are in order here.