Xojo WTF

var x() as integer = New Array(1,2,3,4,5) // error

I wish this was legal. Most LLMs seem to assume it is. Also, declaring an empty array that you can rely on being not nil shouldn’t be such a challenge.

It doesn’t look to be:

Var x() As Integer

It is a challenge in the sense that you have to know that
Var x() As Integer
does not just declare but also instantiates the array, whereas
var myObject as Object
just declares but does not instantiate myObject. So basically you’d have to know that arrays are not treated as objects but as scalar types which might surprise some people (and LLMS).

Moreover, I never understood why it should be “x() As Integer” and not “x as Integer()”.

Because when you use it, it looks like x(5).

Good point!

  • The way Xojo treates an Array is sometimes like a Scalar (integer, string, double etc.). Scalars always exist - they can not need be created with New, and can never be Nil.
  • But sometimes an Array is like an Object (can be Nil).
var x() as Integer // defines and instantiates an Array object which contains no items but is not Nil
var y as Integer // defines and instantiates an Integer which contains zero

var x() as integer = nil // legal
var y as Integer = nil // not legal
1 Like

However, not when defining the return type of a function:

Public Function GetArray() As Integer()
  var x() as Integer = Array(1,2,3,4,5)
  return x
End Function

Compare the As Integer() in the function definition to the x() as Integer in the variable definition.

I think one could argue that

Var x as Integer() 

would be more logical, as it would be consistent with function return values.

2 Likes

I’ve always wished Xojo used square bracket syntax for Arrays…

x = foobar(y)   // is this a function call?  Or is foobar an Array?

Imagine this:

var foobar[] as Integer
x = foobar[y]   // this is clearly an Array and can not be a function call
2 Likes

I wrote a few things together into a blog post:

Arrays in Xojo

2 Likes

I found this article informative, especially about the internal behaviour. Thanks for having written it.

You wrote “To know how many dimensions you have, you can use LastRowIndex with -1 as index. Or better you could do that ten years ago, before it broke.”. I never used multi-dimensional arrays (yet); does your sentence mean there’s no Xojo way to get the number of dimensions these days? It looks to me like an important issue.

Yes, this should be reported and fixed of course.

This was reported long ago in case 54526

IMO, that case should never have reached the “archived” state, for such an essential issue.