Complementing their dynamic environment and event-driven behavior, DataHub scripts have a slightly different approach to symbols and variables compared to other programming languages like C or Java.
One of the fundamental units of Gamma, the DataHub scripting language, is a symbol. Symbols are made up of one or more alphanumeric characters, as well as "_". A symbol gets created whenever a unique group of characters appears in a script for the first time; from that point on the symbol is a unique object in Gamma. When first created, symbols are variables. The value of a variable can be assigned and reassigned at any point in the script. Variables that are not assigned a value have a default value of _undefined_.
Similar to many programming languages, variables in Gamma can be local or global in scope. At the same time, to provide maximum flexibility, variables in Gamma are dynamically typed. Each time a variable is assigned a value, Gamma assigns or reassigns the type for that variable, based on the new value. This facilitates rapid development and eliminates the need to type or even declare all variables before they are used. Of course, good programming principles must still be observed when writing scripts to ensure that the variables are of the correct type for the circumstances.
When a DataHub script is run, Gamma first parses the script, reading and evaluating each statement in turn. For functions or methods, first each argument gets evaluated, and then each statement gets evaluated. Variables get evaluated to their value. Literals like numbers, strings, arrays, and so on get evaluated to themselves. This read/evaluation cycle iterates through the progam on a recursive basis until the entire code gets read and evaluated. When the process is complete, Gamma executes the code.
Sometimes you might not want Gamma to evaluate a statement or variable when it parses the code. For example, when attaching an event handler to the .OnChange method, you don't want the code of the event handler to run until the event actually occurs. To prevent evaluation of a statement or variable, you can use the # character, a Gamma quote operator. Putting the # quote operator in front of any Gamma expression turns it into a literal, causing it to be evaluated to itself, as if it were a number or a string.
Example 1
This example shows an interactive session with Gamma in the Script Log. The --> symbols indicate where the user has input an expression, and the next line shows what Gamma has returned as the result of evaluation.
--> myvar = 5;
5
--> myvar;
5
--> #myvar;
myvar
In some circumstances you might need Gamma to evaluate part of your statement, but not all of it. For this, there are two more quote operators. The ` quote operator indicates that this stament should not be evaluated, except for those places marked by the @ quote operator.
Example 2
In this example, we use the Gamma list function to illustrate how partial evaluation works. The list function creates a space-separated list out of its arguments
--> myvar = 5; 5 --> yourvar = 9; 9 --> list(myvar, yourvar); (5 9)
--> list(#myvar, #yourvar); (myvar yourvar) --> #list(myvar, yourvar); (list myvar yourvar)
--> `list(myvar, yourvar);
(list myvar yourvar)
--> `list(@myvar, @yourvar); (list 5 9) --> `list(myvar, @yourvar); (list myvar 9)
--> string(list(myvar, yourvar)); "(5 9)" --> string(#list(myvar, yourvar)); "(list myvar yourvar)" --> string(`list(@myvar, @yourvar)); "(list 5 9)" --> `string(list(@myvar, @yourvar)); (string (list 5 9)) --> `string(@(list(myvar, yourvar))); (string (5 9))
This the most important example, because this syntax is commonly used with the .OnChange method for handling events. |
class Example Application { ... } method Example.MethodA (x, y) { ... } method Example.constructor () { .OnChange (#V1, `(@self).MethodA (#V1, #V2)); }
In some cases you might need to force Gamma to evaluate an expression. For this, you can use the Gamma eval function. For example:
--> myvar = 5; 5 --> #myvar; myvar --> eval(#myvar); 5
Copyright © 1995-2010 by Cogent Real-Time Systems, Inc. All rights reserved.