Skip to content

Class Members : Symbols and Classes

Alex Rakov edited this page Apr 29, 2019 · 4 revisions

A typical module contains the list of class members : symbols and classes

module_member ::=
   visibility? symbol_attributes? identifier symbol_declaration
   | visibility? class_attributes? identifier class_declaration

Visibility

visibility ::=
   "public"
   | "internal"

A declared symbol or class can be internal or public. The internal symbol cannot be accessed outside its module. If the visibility attribute is skipped the member is internal.

Symbols

Symbols are the named expression. They are used to declare constant symbols, singletons and class symbols

symbol_declaration ::=
   visibility? prefix? scope_attribute? identifier "=" expression ";"

visibility ::=
   "public"
   | "internal"

prefix ::=
   "const"
   | "preloaded"

scope_attribute ::=
   "symbol"
   | "static"

Simple symbols

symbol_declaration ::=
   visibility? "symbol"? identifier "=" expression ";"

A symbol can be used to reuse the expressions:

import extensions;

public symbol InputNumber
    = console.write("Reading a number:").readLine().toInt();

public program()
{
    var n1 := InputNumber;
    var n2 := InputNumber;
    
    console.printLine("The sum of ",n1," and ",n2," is ",n1+n2);
}

The output is:

Reading a number:2
Reading a number:3
The sum of 2 and 3 is 5

Constant symbols

Symbols can be used to implement constants:

public const int N = 2;
public const string Message = "MyMessage";

The symbol expression should be constant one (numeric or string constant for example), otherwise the compiler will generate an error.

Preloaded symbols

Preloaded symbols can be used as a module initializer.

class MyClass1    
{
    foo()
    {
        console.writeLine("foo")
    }
}

class MyClass2
{
    bar()
    {
        console.writeLine("bar")
    }
}

// declaring preloaded symbol, which will be automatically
// invoked on the program start
preloaded startUp = new MyClass1().foo();

public program()
{
    new MyClass2().bar()
}

The output is

foo
bar