What am I misunderstanding about arrays?

The poor OP is probably totally confused at this point!

Let me try to clarify:

First Issue: does variable assignment Copy or Reference?

E.g. what happens when I have two variables, A and B and I say

B = A

Is B now a Copy of A, or is B actually pointing to A ?

In other words, will changes to B affect A, or not?

That’s the concept of “value type” (Scalar type) vs. “Reference type”

In Xojo, many things are Scalar Type:
Integer (and all variations), String, Double,…

For example:

var A as integer = 42
var B as integer = A
B = 99
// at this point, A = 42, and B = 99 - they are not the same thing

With other types (Array, Object…) B is actually a reference to A:

var A() as integer = Array(1,2,3)
var B() as integer = A
B(0) = 99
// at this point, A and B are the same object, an Array containing: (99,2,3)

Second Issue: in a Function (or Method), what happens if you change the Parameter? Do changes get back out of the Function?

This is the concept of “passing by Value” vs. “passing by Reference”

By default in Xojo, all Function or Method parameters are passed ByValue, meaning a copy is made for use inside the function (or Method) and any changes to that value are lost when the function (or Method) ends.

Function AddOne(x as integer) 
  x = x +1
  // x has been increased by one, but that change is lost when the function ends.
End Function

var x as Integer = 9
AddOne(x)
// x is still 9 

vs.

Function AddOne(ByRef x as integer) 
  x = x +1
  // x has been increased by one, and since it's a 'ByRef' parameter, that change gets out of the function.
End Function

var x as Integer = 9
AddOne(x)
// x is 10

1 Like

@Rick_Araujo thank you for that, although I note that they are describing String as “reference type” (which may be true at an implementation level) but at a semantic level, Xojo String is a Value type.

Yet another example of incomplete/incorrect/misleading documentation, although the statement “You don’t have to worry about that now” does give them some wiggle-room, no? :rofl:

2 Likes

Fair enough, but the poster also says:

“Arrays are NOT passed by REFERENCE

They ARE a reference type which is entirely different”

Which is… at a minimum extremely confusing.

1 Like

I think this thread has run its course. ByRef and ByVal are very adequately explored and explained in many other threads on this forum - with better examples and clearer outcomes. This thread hasn’t added anything to the discussion and doesn’t seem likely to do so anytime soon.

OK fair enough on that but you thought I was crazy? :grin:

Yep. Agree that the internal designs deserves a bit more detail for advanced users and even for newbies living unexpected behaviors.

1 Like

Contrary I think it has helped as many might understand certain terms in this post and others.
Has this post solved it for everyone-nope
But hopefully maybe it can help someone in the future

This statement is wrong. The Xojo Compiler was used for the original 32-bit Mac, windows and Linux x86 targets.

LLVM is the compiler for everything after that and the Xojo Compiler is no longer involved.

  • iOS
  • 64-bit
  • ARM

Android uses Android Studio.

3 Likes

That’s what I thought, but some little doubt lingered in my fuzzy brain, so I looked it up and sure enough: what Xojo does when it transforms Xojo code into the LLVM intermediate representation does fit the broad definition of a compiler. From Wikipedia:

“In computing, a compiler is software that translates computer code written in one programming language (the source language) into another language (the target language).”

Now, in my mind, the output of a compiler is executable bytecode, but this definition is broad enough to encompass an awful lot of stuff that we don’t traditionally call compilers. The article does go on to describe subtypes of compilers, including transpilers.

Here is one Video with 2 Xojo engineers talking about Xojo And LLVM

Leveraging LLVM: A Compiler Overview

Good to know.