Yazoo ---> Online Help Docs ---> Yazoo scripting ---> Working with variables

Aliases

The original plan was for Yazoo to be a `neural-network language'. Clearly the program evolved into quite another beast, but most of the organs of the ancient creature are still around in one form or another, having been co-opted for new roles rather than replaced altogether. The neuron became a primitive variable; a grouping of neurons we now call a composite variable; and the synapse that wired those original circuits together turned into something called an alias. After all the layers were added aliases came to have a pivotal backstage role in the language: they are routinely behind the scenes when the user does something like create sets or pass function arguments.

An alias is a variable that shares its data with some other variable. In C the closest equivalent of an alias would be a pointer to another variable. This analogy is not quite exact, however: Yazoo treats a variable and its alias in exactly the same way. The alias does not have to be dereferenced with a `*' or a `->', and does not have a different type specification from its target as in C. In fact, Yazoo itself does not know which was the original variable and which is the alias.

We can define an alias in the following way:


    a := 2   | will default to a signed long
    b :=@ a
    c :=@ b
   

(So not only can we make an alias to `a', but we can also alias its alias, which accomplishes precisely the same thing.) If we were to now print(a), print(b) or print(c), we would uniformly get back the number 2. If we were to set any of the three variables to a different value:


    c = 3
   

then printing any of a, b or c would then cause `3' to be printed.

An alias is not permanent; it can be reassigned to point at another variable. This is done using the equate-at operator `=@'.


    a :: b :: ulong
    a = 2
    b = 3
   
    c :=@ a
    print(c)
    c =@ b
    print(c)
   

Importantly, both a and b have exactly the same type. Had their types been different, retargeting c on the second-to-last line would have caused a type-mismatch error, even if both a and b were numeric. The difference between the :=@ and =@ operators is that the former defines as well as targets aliases; the latter only retargets. (In this case we could actually have used :=@ both times---if the variable it wants to define already exists then it will not complain as long as the types match---but there are certain cases in which we will definitely want to use =@.)

We mentioned that an alias is identical in all respects to the variable it points to. Let's follow this point to its logical conclusion. We just saw that our alias can be reassigned, but Yazoo does not remember which one was the alias. So, we could have reassigned the target of the original variable instead and gotten away with it.


    a :: b :: ulong
    c :=@ a
    a =@ b
   

Yazoo is perfectly happy for us to reassign variables as aliases: all variables are potential aliases. And all aliases are variables. We can get away with the following acrobatics:


    a :: b :: c :: d :: ulong
    a = 2, b = 3, c = d = 4
    a =@ b
    b =@ c
    c =@ d =@ b =@ a
   

So in the end, what numbers do a, b, c and d contain? Clearly they all store the same value---the last line guarantees that. One sensible guess is that the value would be 4, but the correct answer turns out to be 3. The reason: when we reassigned b =@ c, the alias `a' was still left pointing to b's original data, which is the number 3. This follows, again, from Yazoo's ambivalence between the original (b) and the alias (a): if on that line we had instead written via a =@ c then the original `b' would have been unaffected, so why shouldn't reassigning b leave a alone?

To clarify the situation, we need to make the important distinction between members and variables. Until this point we have been rather sloppy on this point, and we shall have to be more careful from now on. A member is a named object in Yazoo: the name of a variable, or a field in a composite variable. The variable itself is the data that the member refers to. In Yazoo these two things are entirely separate, and there is no reason to require a one-to-one correspondence between them. After a =@ b in our last example we have two members, a and b, pointing to one variable: the original variable of b which holds the number 3. The subsequent b =@ c simply retargets member b, but that has nothing whatsoever to do with member a, which remains firmly pointed at that same number-3 variable.

The equate operator `=' copies data between two variables, and it has a natural counterpart in the comparison operator `==' which tests for equality of the data between two variables. The equate-at operator `=@' copies the reference between two members. To complete the symmetry, Yazoo also has a reference-comparison operator `==@' which tests to see whether two aliases point to the same thing. A reference-comparison will return true if and only if the two members have to point to the same variable, and if that variable is part of an array (see next section) then the members must point to the same array elements. Finally, there is a reference-not-equal operator `/=@' which is the logical negation of `==@'.

One syntactic point: we can give our scripts a dash more elegance by putting space (as much as we want) before the `@' of any of the aliasing operators. So a couple of lines from one of our earlier examples could have been written


    c := @a
    a = @b
   

The extra space makes it clearer that the relationship between `=@' and `:=@' is analogous to that between `=' and `:='. It also highlights the similarity between `=@' and `=': in both cases the operation will cause the left-hand member to point to the value contained in the right-hand argument, only by different means. For similar reasons we may prefer to write our reference comparisons as if a == @b or if a /= @b.

The extra space before the alias symbol may tempt suspicion that just the `@' is by itself some new alias operator, something maybe similar to the address-of operator `&' in C -- but now we have pushed the analogy too far. The @ symbol by itself is meaningless; it is only the last character of the four aliasing operators.


Prev: Other equate operators   Next: The void


Last update: July 28, 2013

Get Yazoo scripting language at SourceForge.net. Fast, secure and Free Open Source software downloads