Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Version History

« Previous Version 11 Next »

 

Units

Units are groupings of functions and variables appearing in .mez files. They can be thought of as modules or namespaces. A Helium app must have at least one unit. The unit keyword must be followed by a unique unit name.

 

Unit Example
unit MyUnit;
// Other code follows
 
string myVar;
 
int factorial(int x) {
    if(x == 0 || x == 1) {
        return 1;
    } else if (x > 1) {
        return factorial(x - 1);
    }
 
    return 0;
}

 

 

Objects, enums and validators are global, so they can optionally be in a separate source code file without a unit.

Mez File Comments

Two types of comments are supported: single line comments and multi-line comments:

Single Line Comments Example
// date tstamp = System.now();
// decimal rand = System.random();
Multi-line Comments Example
/* date tstamp = System.now();
decimal rand = System.random(); */

 

 

 

Variable and Function Scope

This unit has one unit variable named myVar. This variable has unit scope and can be accessed by any function in the unit. The unit also has a function named factorial. A unit must have at least one function. To refer to a unit’s member functions or variables from another unit, you have to use the scope operator (the colon).

unit TestUnit;
void test() {
    MyUnit:myVar = "New string value!";
    int f = MyUnit:factorial(5);
}

 

 Note: Unit variables cannot be in line initialized when they are declared. The following code will not compile:

unit MyUnit;

string myVar = "some value"; // this is not a legal statement

 


 

Additional Mentions and References

 

  • No labels