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

Composite variables

All variables in Yazoo are either primitive or composite. The composite variable of Yazoo is quite a multifaceted object, but from our present perspective it is simply a collection of primitive variables. It is the analog of the struct in C. You can define composite types and create variables of those types in the following way:


    StreetAddressType :: {
       number :: ulong
       street :: string
    }
   
    PetersPlace :: StreetAddressType
   

The first four lines define the composite StreetAddressType, using the same operator (::) that we use for defining data variables. Here the type name goes to the left of `::', and the type definition goes to the right in braces. Line breaks (or commas) separate the fields (members) in the type definition from each other (and, optionally, from the closing brace); however, the opening brace must appear on the same line as the define symbol ::. In the final line, StreetAddressType is used to coin the new variable PetersPlace.

As in C, we can use a period to access the various members of a composite variable. Thus after defining PetersPlace we might initialize it with something like


    PetersPlace.number = 357
    PetersPlace.street = "Gooseberry Drive"
   

Types may be nested within one another, either by using previously-defined types within the definition of a new type, or by inlining one type's definition inside another's. Both methods are shown below.


    FullAddress :: {
       first_line :: StreetAddressType
       second_line :: {
           city :: state :: string
           zip :: ulong
       }
    }
   

In this case, we would have to make double use of the `.' to access any of the primitive fields.


    mailing_address :: FullAddress
   
    mailing_address.first_line.number = 357
    ...
   

Yazoo even allows the user to use types that were defined inside other types.


    general_whereabouts :: FullAddress.second_line
   
    general_whereabouts.city = "Detroit"
    ...
   

One peculiarity of composite variables is that their internal structures can be rearranged after they have been defined. For example:


    PaulineAddress :: FullAddress
   
    remove PaulineAddress.first_line.street   | it's a PO box
    PaulineAddress.country :: string          | in another country
   

This should be pretty self-explanatory: by the end PaulineAddress will have a third member (as if country :: string had appeared inside the definition of FullAddress); also the street field will be missing. Evidently delete removes members within variables as well as whole variables.

There are instances in which a composite variable will be created without an explicit type definition. Suppose that, out of the blue, we were to define


    (James.first_line.street :: string) = "Halfway Ave."
   

without ever having defined James :: FullAddress. In Yazoo this is quite legal (though it wouldn't be if this were the only use for it)---we shall call it an implicit variable definition, and it basically accomplishes the following:


    empty_type :: {}
   
    James :: empty_type
    James.first_line :: empty_type
    James.first_line.street :: string
    James.first_line.street = "Halfway Ave."
   

In other words, when Yazoo has to step through a variable which doesn't exist in the process of defining a new member, it will create an empty composite variable to which it will then immediately add a member. The left-hand argument of a define operator is the only circumstance in which members are created implicitly. The difference between a composite variable defined with one member and an initially empty variable with an added member is indeed significant, as we will see presently.

Most non-numeric operations that can be applied to primitive variables also work with whole composite variables. For example, the following code is quite acceptable, since both the equate `=' and comparison `==' operators can take non-numeric arguments provided that their types match.


    Tom :: Bob :: StreetAddressType
    ...
    Tom = Bob
    if Tom == Bob     | yes, they will be equal
       print("They're sharing a room.\n")
    endif
   

In this context, two variables have matching types only if their fields are identical, or can be straightforwardly converted (i.e. having corresponding fields of different numeric types). So had we defined Bob separately using a BobsAddressType that we wrote just for him, we would be able to copy and compare Tom and Bob if and only if BobsAddressType consisted of a (any) numeric type followed by a string.


   

* * *

Thus far we've been talking as if types and composite variables are actually two different things in Yazoo. In fact they're not. So, we can write things like:


    Tom :: {
       number :: ulong
       street :: string   }
   
    Tom.number = 63
    remove Tom.street
   
    Bob :: Tom
   

What just happened? First, when we defined Tom we were actually defining a composite variable with such-and-such structure, not a type per-se. Second: the define statement of the last line created the new variable Bob---read---"of the same type as variable Tom", not "of type Tom". More precisely, Bob is defined as Tom was originally defined, which is a ulong set to zero followed by an empty string.

So there is no distinction between, say, Tom and StreetAddressType from our earlier example: both are identically-structured composite variables. The fact that one happened to be defined directly and the other happened to have its definition copied from another variable is immaterial as far as Yazoo is concerned. One wonders whether we might also be able to define primitive variables off of each other too:


    num1 :: ulong
    num2 :: num1
   

Indeed we can. The conclusion: variable `type' is some intrinsic quality of variables -- no hard object that is only `type' exists in Yazoo. (Though pretending otherwise, as we were doing earlier, can be a useful fiction if it improves the readability of our code.)


Prev: Primitive variables   Next: Other equate operators


Last update: July 28, 2013

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