Passing an Array to a Method: By Ref or Not

Taken with the other comment that “a reference is a pointer”, my issue with this terminology is that references in Xojo cannot refer to other references. Consider the following:

Var a as Dictionary
Var b as Dictionary
Var c as Dictionary

a=new Dictionary
b=a
c=b

a=another new Dictionary
//b still refers to the first Dictionary
//c still refers to the first Dictionary

There’s no way to construct this using Xojo references such that c and b “follow” a when a is changed to refer to a new object. This is what “references to references” would imply, but it simply doesn’t exist in the language.

If you are coming from a pointer-based language like C++, and you read that Xojo references are pointers, and that there are such things as references to references, you are inevitably led to the notion that “pointers that point to pointers” are a thing in Xojo and you proceed on that erroneous assumption.

Your example is a different use case than the OP. Perhaps that’s why this thread got so convoluted. Using ByRef in a method signature does create a reference to a reference.

1 Like

This behavior is consistent back to 2010r5.1

I guess we must be using this terminology in ways that don’t correspond to the same concept. In my conception, this isn’t a thing that is possible in the Xojo language.

I compiled for ARM and disassembled. It looks exactly like I would expect.

Array passed ByVal:

var1 = CreateArray()
...
(function pointer)(..., var1)

Array passed ByRef:

var1 = CreateArray()
...
(function pointer)(..., &var1)

So the result of creating an array is a pointer. Passing by value passes that pointer. Passing by reference passes a pointer to the pointer.

This seems like where the difference in perspective lies. The array is referenced with an object pointer—the address where the array is stored in memory. With ByVal the value of that address is passed to the function. I’m okay with calling that a “new reference to the object”. If you use ByRef, you are passing a pointer to a pointer—the address of where you store the address of the array object in memory. I wouldn’t call this “the original reference” as it is one level removed from the reference to the array.

2 Likes

These are great points and I agree that it would be confusing and expectations might be different from the actual behavior. Passing an array with ByRef to a function is different than this example though.

I agree that you can’t chain a, b, and c to “follow” as they are all Dictionary references. But you can of course use OOP to establish that kind of relationship where c is a thing that references b which is a thing that references a which is a thing that references a Dictionary. For example:

https://travi.sh/Ref.xojo_binary_project-4FvuV1mpDN.zip

Which outputs

                RefA -> Dict : Example
        RefB -> RefA -> Dict : Example
RefC -> RefB -> RefA -> Dict : Example
                RefA -> Dict : Test
        RefB -> RefA -> Dict : Test
RefC -> RefB -> RefA -> Dict : Test

We’re really getting into the assembly language weeds here. While what you’ve found in the opcodes is interesting, and I don’t want to discount that, my whole point is not how the compiled code is structured; it’s how we mentally model the way the Xojo language behaves, and what its capabilities are. It doesn’t have some of the attributes of pointer-based languages, and we should be careful to not overlap our terminology when we describe how the language is structured and how it behaves.

One of the reasons I’m pounding this drum so hard is that how Xojo handles references is a very common stumbling block for new users, as can be seen in this very forum (and this thread). Either they are new to references altogether, or they are coming from another language that handles such concepts differently, and there’s a learning curve. We can be maximally helpful to them by being as clear as possible when describing the system.