I was reading some of the $languageWTF sites which are amusing and educational, as they talk about edge-cases and weird “footguns” in the languages. For example:
- GitHub - denysdovhan/wtfjs: 🤪 A list of funny and tricky JavaScript examples
- GitHub - satwikkansal/wtfpython: What the f*ck Python? 😱
I used to love Python, but reading these has made me appreciate Xojo’s langauge even more - I think Xojo (the langauge) has relatively few of these WTF moments.
Has anyone collected a WTF Xojo page?
If not, how about putting your favorite one here…
For purposes of discussion, let’s limit these purely to weird things in the core Xojo programming language. Let’s stay away from framework, IDE, or per-platform/OS bugs.
Here are a few of mine:
dim d as new dictionary
dim a(0) as integer
for i as integer = 1 to 3
a.ResizeTo(0)
a(0) = i
d.Value(str(i)) = a
next
// what does D contain?
dim j as new JSONItem(d)
log J.ToString
// {"1":[3],"2":[3],"3":[3]}
Diagnosis:
Arrays are weird. They function somewhat like Objects, but unlike Objects you can’t “new” them, and resizing an array does not create a new instance of the array. As a result, the dictionary contains 3 references to the same array.
Solution:
define the array within the loop - variables defined inside a loop are created as new instances for each iteration:
dim d as new dictionary
for i as integer = 1 to 3
dim a(0) as integer
a(0) = i
d.Value(str(i)) = a
next
// what does D contain?
dim j as new JSONItem(d)
log J.ToString
// {"1":[1],"2":[2],"3":[3]}
Now the Dictionary contains 3 different arrays.