5. Definitions

5.1. Function Definitions

Syntax

FunctionDefinition ::=
    FunctionParameterList (-> Type)? => FunctionBody

FunctionBody ::=
    NonDeclarativeExpression

FunctionParameterList ::=
    FunctionParameter (, FunctionParameter)* ,?

FunctionParameter ::=
    MacroInvocationHeader? FunctionParameterContent

FunctionParameterContent ::=
    Name : Type (= NonDeclarativeExpression)?
    | Name : Type? = NonDeclarativeExpression

Examples

foo := (i: i32, j: i32) -> i32 => {
    i + j
}

main := () => ()

5.2. Struct Definitions

Syntax

StructDefinition ::=
    struct DefinitionParameterList? ( StructFieldList )

StructFieldList ::=
    StructField (, StructField)* ,?

StructField ::=
    MacroInvocationHeader? StructFieldContent

StructFieldContent ::=
    Name : Type? = NonDeclarativeExpression
    | Name : Type
    | Type

Examples

Foo := struct<T>(
    x: T,
    y: T = 0
    z: f32
)

#[repr("c")]
SizedPointer := struct(&raw u8, usize)

5.3. Enum Definitions

Syntax

EnumDefinition ::=
    enum DefinitionParameterList? ( EnumVariantList )

EnumVariantList ::=
    EnumVariant (, EnumVariant)* ,?

EnumVariant ::=
    MacroInvocationHeader? EnumVariantContent

EnumVariantContent ::=
    Name EnumVariantFieldsList? (: Type)?

EnumVariantFieldsList ::= ( ParameterList )

Legality Rules

5.3:1 A enum type is an abstract data type that contains enum variants.

5.3:2 A zero-variant enum type has no values.

5.3:3 An enum variant is a construct that declares one of the possible variations of an enum.

5.3:4 The name of an enum variant shall be unique within the related EnumDefinition.

5.3:5 A discriminant is an opaque integer that identifies an enum variant.

5.3:6 A discriminant initialiser shall be specified via the discriminant attribute.

5.3:7 The type of the expression expression of a discriminant initialiser shall be determined as follows:

  1. 5.3:8 The type of the primitive representation specified by the attribute repr.

  2. 5.3:9 Otherwise, the smallest integer type that can represent the full range of discriminant values. If the range of discriminant value does not contain any :t`negative integer` values, then the type of the discriminant shall be the smallest unsigned integer type.

5.3:10 The value of a discriminant of an enum variant is determined as follows:

  1. 5.3:11 If the enum variant has an applied discriminant initialiser, then the value is the value of its expression.

  2. 5.3:12 Otherwise, if the enum variant is the first enum variant in the EnumVariantList, then the value is zero.

  3. 5.3:13 Otherwise, the value is one greater than the value of the previous enum variant.

5.3:14 It is a static error if two enum variants have the same discriminant.

5.3:15 It is a static error if the value of a discriminant exceeds the maximum value of the type of the expression of a discriminant initialiser.

Examples

Foo := enum<T>(
    Bar,
    Baz(i32),
    Qux(i32, f32)
)
ErrorCode := enum(
    None,
    InvalidArgument,
    InvalidState,
    InvalidOperation,
    #[display_name("Invalid Data")]
    InvalidData
)
FloorId := struct(i8);

EmployeeKind := enum(
    #[discriminant(7)]
    FactoryWorker(FloorId),
    #[discriminant(12)]
    Engineer,
    #[discriminant(83)]
    Hardware,
    #[discriminant(666)]
    Manager,
)

5.4. Implicit Function Definitions

Syntax

ImplicitFunctionDefinition ::=
    DefinitionParameterList (-> Type)? => ImplicitFunctionBody

ImplicitFunctionBody ::=
    NonDeclarativeExpression

Examples

identity := <T> => (t: T) -> T => t

identity(3) == 3 == identity<i32>(3)

5.5. Traits

Warning

This is work in progress and not yet implemented.

5.6. Module Definitions

Syntax

ModuleDefinition ::=
    mod DefinitionParameterList? { ModuleMemberList }

ModuleMemberList ::=
    StatementList

Examples

pub nested := mod {
    pub Colour := enum(Red, Green, Blue)

    MixedColour := struct(u32);

    priv combine_colours := (a: Colour, b: Colour) -> MixedColour => {
        ...
    }
}

5.7. Implementation Definitions

Warning

This is work in progress and not yet implemented.

5.8. Definition Parameters

Syntax

DefinitionParameterList ::=
    < TypeParameterList? >