# Xojo Tip : NIL Arrays

**URL:** <https://ifnotnil.com/t/xojo-tip-nil-arrays/4106>\
**Category:** Xojo\
**Tags:** tips, xojo\
**Created:** [29 August 2024 17:05 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106 "2024-08-29T17:05:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [29 August 2024 17:05 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/1 "2024-08-29T17:05:38Z")

</div>

New post about dealing with NIL arrays in Xojo  
Not EMPTY arrays, which are not nil, as Xojo docs get wrong in places

[https://www.great-white-software.com/blog/2024/08/29/xojo-and-nil-arrays/](https://www.great-white-software.com/blog/2024/08/29/xojo-and-nil-arrays/)

---

<div class="post-metadata">

**Author:** ![Torsten\_B](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@Torsten\_B](https://ifnotnil.com/u/Torsten_B)\
**Post date:** [29 August 2024 22:19 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/2 "2024-08-29T22:19:19Z")

</div>

Ahem, the Xojo compiler doesn’t catch this and doesn’t issue a warning?

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 August 2024 03:04 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/3 "2024-08-30T03:04:37Z")

</div>

Not at all

---

<div class="post-metadata">

**Author:** ![anic297](https://ifnotnil.com/user_avatar/ifnotnil.com/anic297/32/18_2.png) [@anic297](https://ifnotnil.com/u/anic297)\
**Post date:** [30 August 2024 12:05 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/4 "2024-08-30T12:05:43Z")

</div>

> [@Torsten\_B](#):
>
> Ahem, the Xojo compiler doesn’t catch this and doesn’t issue a warning?

How would it, for example, catch the following code?

```auto
Function MyMethod(Param1 as integer) as String()
Var ReturnValue() as string

if MyParameter=1 then Return ReturnValue
End function

```

(where nil is returned if MyParameter\<\>1)

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 August 2024 13:47 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/5 "2024-08-30T13:47:48Z")

</div>

I think many people assume that “the function will return the default value for its return type”

Which in most cases is true

Except what IS the default value for an array return ?  
Turns out its NIL - NOT an empty array

Could the compiler return an empty array ?  
I suppose it could be altered to do so

Would it break existing code ?  
Possibly but I truly have no idea

My post was just to let people know how to handle it IF they encountered this situation as it is a bit of an oddity when you get used to the fat that by default a method will return the default for the return type if you do nothing else

---

<div class="post-metadata">

**Author:** ![Torsten\_B](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@Torsten\_B](https://ifnotnil.com/u/Torsten_B)\
**Post date:** [30 August 2024 13:49 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/6 "2024-08-30T13:49:59Z")

</div>

I see, catching nil is entirely with the developer. The language itself doesn’t offer mechanisms that protect against non-intentional nil results.

---

<div class="post-metadata">

**Author:** ![anic297](https://ifnotnil.com/user_avatar/ifnotnil.com/anic297/32/18_2.png) [@anic297](https://ifnotnil.com/u/anic297)\
**Post date:** [30 August 2024 14:21 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/7 "2024-08-30T14:21:20Z")

</div>

> [@npalardy](#):
>
> Except what IS the default value for an array return?  
> Turns out its NIL - NOT an empty array

Since, as you said, an array is an object under the hood, I don’t see anything wrong with the behaviour of returning a nil object in such cases.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 August 2024 14:34 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/8 "2024-08-30T14:34:20Z")

</div>

It might just be surprising to many since you dont encounter NIL arrays very often

Empty ones no problem and I’m sure we’ve all run into those

Again, my post was just a _heads up + how to_ for folks who might not have run into them

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [30 August 2024 14:46 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/9 "2024-08-30T14:46:06Z")

</div>

In Swift is seems this is not possible

```auto
 func myMethod(p : Int) -> [String] {
        var returnValue : [String] <--- used before initalized
        if p==1 { return returnValue }
<- missing return statement 
    }

```

Compiler emits multiple errors (that I guess Xojo does not)

if the user wanted to do this ON PURPOSE,

```auto
func myMethod(p : Int) -> [String] {
        var returnValue : [String]?
        if p==1 { return returnValue! }
        return returnValue!
    }

```

---

<div class="post-metadata">

**Author:** ![anic297](https://ifnotnil.com/user_avatar/ifnotnil.com/anic297/32/18_2.png) [@anic297](https://ifnotnil.com/u/anic297)\
**Post date:** [30 August 2024 14:48 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/10 "2024-08-30T14:48:56Z")

</div>

I agree. I’m accustomed to nil arrays myself and they make absolute sense, IMO.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 August 2024 15:04 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/11 "2024-08-30T15:04:07Z")

</div>

> [@DaveS](#):
>
> Compiler emits multiple errors (that I guess Xojo does not)

No Xojo has never caught the exit of a function without returning a value explicitly  
It might be nice but how much code would that affect ? 😱

Maybe another warning that can be turned on turned off

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 August 2024 15:10 UTC](https://ifnotnil.com/t/xojo-tip-nil-arrays/4106/12 "2024-08-30T15:10:21Z")

</div>

I dont find it surprising they make sense to you  
You’ve used Xojo for a long time  
But to many it makes little sense since arrays arent “objects” in Xojo (you never NEW one)  
And you cant get one in “normal” flow

```auto
dim v() as string
dim s() as string = Array("")

```

Neither will give you a NIL array - an empty one sure

A missing return from a function that returns an array is one of the ways you will get one. There might be other ways like pulling an array out of a variant thats has been assigned a nil value. Or maybe a plugin (not sure about this one)

So again it was just a heads up
