definitions
variables
In ghūl variables are introduced with the let keyword:
The compiler will infer the type from the initializer, if there is one. If there is no initializer, then a type must be explicitly specified.
If both an initializer and a type are present, then the initializer must be assignment compatible with the type
Multiple variables can be defined in the same let statement, including a mix of types and with or without initializers
The name _ is a discard placeholder. It can stand in for any variable name, but the value that would be assigned to it is discarded. _ is accepted in let definitions, tuple destructuring, lambda parameters, and for loop variables:
Variables may only be defined within functions, methods or property bodies. Variables names should be in snake_case
functions
In ghūl functions consist of a name and a parenthesized formal arguments list, followed by a return type, and then either a return expression or a function body:
=> introduces a single-expression body, while the is and si keywords are used to delimit block bodies.
Functions can only be defined at global scope. Functions can be generic, which will be covered later. Function names should be in snake_case
arguments
Arguments consist of a name followed by a type. The type is mandatory as the compiler cannot infer types here.
types
classes
Classes consist of a name optionally followed by a superclass name and the types of any traits implemented, and then the class body. The class body is delimited by keywords is and si:
A class defines a new reference type, instances of which are assignment compatible with its superclass type and any traits it implements.
Instances of classes are created via a constructor expression, which consists of a type expression followed by a parenthesis delimited list of actual constructor arguments. For a class, the type expression is simply the class name, qualified with any namespaces if needed:
Classes can only be defined at global scope. Classes can be generic, which will be covered later. Concrete class names should be in MACRO_CASE. Abstract class names should be in PascalCase.
structs
Structs consist of a name, then the types of any traits implemented, and then the struct body again enclosed in is / si:
Structs are constructed the same way as classes, with a constructor expression:
A struct defines a new value type, which means any values that the struct encapsulates are collected together as a new kind of value: copying the struct involves copying all the encapsulated values and the built in equality operator == performs a memberwise equality check:
Structs can only be defined at global scope. Structs can be generic, which will be covered later. Struct names should be in MACRO_CASE.
traits
A trait consists of a name, the types of any parent traits that must also be implemented, and then the trait body:
Traits are similar to interfaces in other languages. Trait methods and properties without a default implementation must be implemented by any class that inherits from the trait:
A trait method or property can provide a default body. Implementing classes inherit the default and only need to override it to change the behaviour:
A class override can call the trait's default with super.method().
Traits can only be defined at global scope. Trait methods and properties can be abstract or have a default implementation. Trait names should be in PascalCase.
unions
A union consists of a name and then a union body, which contains one or more variants. Each variant has a name, and then an optional list of fields:
Unions are a reference type. A reference of union type can point to only one variant at a time. The active variant can be determined by checking the union's tag properties. These are auto named by convention based on the variant names.
have tree node
The active variant can be accessed by name, which returns either the variant instance (if it holds multiple fields), or just the field value if it holds a single field. Unit variants (those with no fields) cannot be accessed as they have no value.
Unions support structural equality through the =~ operator. Two union references compare equal when they hold the same variant with member-wise equal fields:
Unions can only be defined at global scope. Union names should be in PascalCase and variant names should be in MACRO_CASE
enums
An enum consists of a name and then an enum body, which contains one or more elements. Each element has a name and an optional constant integer value
Enums can only be defined at global scope. Enums and their members should be named in MACRO_CASE
properties
A property consists of the property name followed by the property's type and, optionally, bodies for getter and setter methods.
Public properties with no getter or setter are automatically backed by a hidden field. Private properties with no getter or setter are implemented as a plain field.
Properties can be defined within globally and within classes, structs and traits. Property names should be in snake_case.
methods
Methods are syntactically the same as functions, except they are defined within classes, structs or traits.
As with functions, methods should be named in snake_case
constructors
In ghūl methods named init are constructors. When an object is constructed using a constructor expression, the corresponding init method overload will be called based on the actual argument types
Constructors can be defined in classes and structs
namespaces
Namespaces are introduced with the namespace keyword followed by the namespace name and then the namespace body.
Namespaces may be nested inside other namespaces
did something
A dotted namespace name is shorthand for nesting namespaces
did something
namespace aggregation
A namespace definition is an instance of that namespace. Namespace instances are aggregated across all source files to form a single namespace scope. This means that all definitions within a namespace instance are visible unqualified within all other instances of that namespace in all source files:
source-file-1.ghul:
source-file-2.ghul:
definitions outside any namespace
If a source file contains no namespaces, then all definitions in the file are placed in a compiler generated namespace that is private to that source file. This is useful for examples and tests:
Hello, world!
For definitions to be visible from other files, they must be placed in an explicitly declared namespace.
namespace usage consistency
If a source file contains any explicitly declared namespaces, then all definitions in that file must be within a namespace. Bare definitions outside of namespaces are not allowed in files with namespace declarations:
importing symbols with use
Symbols can be brought into the current namespace instance's scope using the use keyword. Imported symbols can then be used without qualification:
use applied to a namespace imports all symbols from that namespace:
Note that use only applies within the current namespace definition. It does not import a symbol into all instances of the current namespace:
visibility of symbols
In ghūl, the visibility of symbols outside their defining scope is managed by a naming convention which is partially enforced by the compiler
global symbols
Classes, structs, traits, unions, global functions and global properties are accessible from any namespace. Prefixing their names with _ indicates they are intended to be private, but this is not enforced by the compiler:
methods
Methods are public by default. To make a method protected, prefix its name with an underscore _:
Protected access to methods is enforced by the compiler
properties
Properties are public read, protected write, unless they start with _, in which case they are protected read and write:
planned changes
Protected access will become private in a future release: derived types should not rely on reading or writing members with _ prefixed names