# Xojo WTF

**URL:** <https://forum.xojo.com/t/xojo-wtf/70510>\
**Category:** General\
**Created:** [June 1, 2022, 1:56pm UTC](https://forum.xojo.com/t/xojo-wtf/70510 "2022-06-01T13:56:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [June 1, 2022, 1:56pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/1 "2022-06-01T13:56:05Z")

</div>

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](https://github.com/denysdovhan/wtfjs)
- [GitHub - satwikkansal/wtfpython: What the f\*ck Python? 😱](https://github.com/satwikkansal/wtfpython)

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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [June 1, 2022, 2:01pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/2 "2022-06-01T14:01:30Z")

</div>

Well, the behavior shown is what I would expect, so not a WTF?

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [June 1, 2022, 2:02pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/3 "2022-06-01T14:02:56Z")

</div>

**Strings** are also a bit weird

```auto
dim a as string = "Alpha"
dim b as string = a
b = "Beta"
log "a=" + a + " b=" + b
// a=Alpha b=Beta

```

Strings are immutable and assignment creates a new String.

Compare this to **Array** s, in which 2 array variables can point to the same object:

```auto
dim a(0) as string
a(0) = "Alpha"
dim b() as string = a
b(0) = "Beta"
log "a(0)=" + a(0) + " b(0)=" + b(0)
//a(0)=Beta b(0)=Beta

```

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [June 1, 2022, 2:05pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/4 "2022-06-01T14:05:23Z")

</div>

> [@Rick\_Araujo](#):
>
> Well, the behavior shown is what I would expect, so not a WTF?

The point being that Arrays and Strings have different semantics in Xojo - after many years, I’m perfectly used to this and think it makes sense, but it can be unexpected behavior depending on your expectations from other languages…

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [June 1, 2022, 2:05pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/5 "2022-06-01T14:05:32Z")

</div>

> [@Mike\_D](#):
>
> **Strings** are also a bit weird

The behavior shown is the same behavior of the first case, array related, not string related. To me, Strings aren’t weird.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [June 1, 2022, 2:08pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/6 "2022-06-01T14:08:58Z")

</div>

> [@Mike\_D](#):
>
> The point being that Arrays and Strings have different semantics in Xojo

To be true, strings and arrays have different semantics in 99% of high level languages. They approach each other in lower level languages where both became pointers to mutable contents.

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [June 1, 2022, 2:11pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/7 "2022-06-01T14:11:08Z")

</div>

> [@Rick\_Araujo](#):
>
> To be true, strings and arrays have different semantics in 99% of high level languages.

Agree, though if you read about the [Python String WTFs](https://github.com/satwikkansal/wtfpython#-strings-can-be-tricky-sometimes), you will be very happy to not run into these issues in Xojo. (Shorter version: in Python, two strings may, or may not refer to the same object, depending on their contents. Ugh)

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [June 1, 2022, 2:19pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/8 "2022-06-01T14:19:05Z")

</div>

The pointed Python cases are really WTF cases. Erratic, non expected, inconsistent behaviors depending on Python versions and string contents.

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [June 1, 2022, 2:46pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/9 "2022-06-01T14:46:36Z")

</div>

Another WTF is that [Operator\_Convert — Xojo documentation](https://documentation.xojo.com/api/math/operator_convert.html#operator-convert) can get you into trouble. Discussed a number of times previously. Some of the “gotchas” are:

- Operator\_Convert in a superclass can return an instance of a subclass
- Operator\_Convert creates a new object instance, but does not call Constructor(). This can lead to instances that are not properly initialized.
- Operator\_Convert with nil objects can be confusing, for example see [Xojo: Account Login](https://tracker.xojo.com/xojoinc/xojo/-/issues/64272)

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [June 1, 2022, 3:12pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/10 "2022-06-01T15:12:35Z")

</div>

Variants and the **is** operator can be a little surprising.

```auto
dim s1 as variant = "foo"
dim s2 as variant = "bar"
s2 = "foo"

if s1 = s2 then
  log "s1 = s2"
else
  log "s1 <> s2"
end if

if s1 is s2 then
  log "s1 is s2"
else
  log "s1 is not s2"
end if

// output:
// s1 = s2
// s1 is s2

```

I’m not sure about this one. The documentation for “is” says

> The Is operator returns [True](https://documentation.xojo.com/api/language/true.html) if _object1_ and _object2_ actually refer to the same object. It checks identity, not contents, so it Is not affected by the presence of a comparison operator.

However, if you do this:

```auto
dim s1 as variant = "foo"
dim s2 as variant = "bar"
dim a(0) as string = Array("foo")
s2 = a(0)
// output:
// s1 = s2
// s1 is not s2

```

I wonder if the compiler is optimizing string constants in the first case but not the second?

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [June 1, 2022, 3:17pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/11 "2022-06-01T15:17:33Z")

</div>

Also, a variation on the **Array** one above - turns out the Array() function does create a new array instance:

```auto
dim d as new dictionary

dim a() as integer
for i as integer = 1 to 3
  a = Array(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]}

```

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [June 1, 2022, 3:31pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/12 "2022-06-01T15:31:21Z")

</div>

> [@Mike\_D](#):
>
> ```auto
> dim s1 as variant = "foo"
> dim s2 as variant = "bar"
> s2 = "foo"
> 
> if s1 = s2 then
> log "s1 = s2"
> else
> log "s1 <> s2"
> end if
> 
> if s1 is s2 then
> log "s1 is s2"
> else
> log "s1 is not s2"
> end if
> 
> // output:
> // s1 = s2
> // s1 is s2
> 
> ```

Seems kind of the same Python quirk. The compiler used a pointer to the same internally “fused” in just one literal.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [June 1, 2022, 3:38pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/13 "2022-06-01T15:38:30Z")

</div>

Presumably the take-away is that if one is presented with a variant whose known content is a string, and the intention is to further manipulate the string, it’s best to copy it to a string first.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [June 1, 2022, 3:39pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/14 "2022-06-01T15:39:33Z")

</div>

I just prefer it working as expected instead.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [June 1, 2022, 3:42pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/15 "2022-06-01T15:42:03Z")

</div>

You mean you would like a variant which contains a string to behave idenically to a string?

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [June 1, 2022, 3:48pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/16 "2022-06-01T15:48:20Z")

</div>

I mean that manual states that

Var a As Variant = 0 // One variant variable  
Var b As Variant = 0 // Another variant variable  
Var r As Variant

should end as

r = a = b // True  
r = a is b // False

But it ends as

r = a = b // True  
r = a is b // True, as if(a is a) and not if(a is b)

The same quirk some Python versions have.

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [June 1, 2022, 6:52pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/17 "2022-06-01T18:52:09Z")

</div>

Here’s my WTF:

```xojo
Var Value As UInt32 = 4294967295
If Value > 100 Then
  // Won't Reach Here
End If

```

---

<div class="post-metadata">

**Author:** ![Aslan\_Babakhanov](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/b487fb/32.png) [@Aslan\_Babakhanov](https://forum.xojo.com/u/Aslan_Babakhanov)\
**Post date:** [June 1, 2022, 9:38pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/18 "2022-06-01T21:38:16Z")

</div>

This is strange. I wonder how it was translated by Xojo…  
Its working if compile in 64bit mode only, but should work in 32bit too

---

<div class="post-metadata">

**Author:** ![KarenA](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/c89c15/32.png) [@KarenA](https://forum.xojo.com/u/KarenA)\
**Post date:** [June 1, 2022, 9:42pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/19 "2022-06-01T21:42:10Z")

</div>

> [@Thom\_McGrath](#):
>
> ```auto
> Var Value As UInt32 = 4294967295
> If Value > 100 Then
> // Won't Reach Here
> End If
> 
> ```

So instead of converting the 100 to a Uint32 for the comparison , the compiler converts Value to integer? Not great behavior…

When I first started with REALBAsic , there were no unsigned datatypes.

On Bob’s old blog I made a comment about UInts types seemed to be bolted onto the framework and not well integrated. A former Xojo engineer that was involved in adding them to the framework (maybe Aaron?) , basically agreed… which is why one finds such edge cases still all these years later.

-Karen

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [June 1, 2022, 9:45pm UTC](https://forum.xojo.com/t/xojo-wtf/70510/20 "2022-06-01T21:45:19Z")

</div>

Xojo does not take sign in consideration when evaluating expressions, it assumes everything as signed values, so if the sign bit is set in a unsigned positive value it becomes a negative value being evaluated. I would say a bug. Old one.

[Next page](https://forum.xojo.com/t/xojo-wtf/70510.md?page=2)
