Xojo Tip : NIL Arrays

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/

6 Likes

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

Not at all

1 Like

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

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)

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

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

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.

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

In Swift is seems this is not possible

 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,

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

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

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 ? :scream:

Maybe another warning that can be turned on turned off

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

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

1 Like