Skip to main content

Type Annotation with as

The as keyword explicitly specifies the type of a variable, parameter, or return value.


Variable Type Annotation

counter is 0 as int;
ratio is 0.5 as float;
name is "Alice" as string;
flag is true as bool;

Without as (Type Inference)

When as is omitted, the compiler infers the type automatically:

counter is 0;        // inferred as int
ratio is 0.5; // inferred as float
name is "Alice"; // inferred as string

See Type Inference for details.


Method Parameter Types

Parameters use name as type syntax:

Add method(a as int, b as int) as int {
return a + b;
}

Method Return Type

Use as after the parameter list to specify the return type:

GetName method() as string {
return "Morph";
}

// No return type (void)
PrintHello method() {
Print("Hello!");
}

Class Field Types

Player class {
name is "" as string;
health is 100 as int;
speed is 1.0 as float;
}

When to Use Explicit Types

SituationRecommendation
Type is obvious from valueOmit asx is 10;
Type needs clarificationUse asx is 0 as float;
Method parametersAlways specify types
Method return valuesAlways specify if non-void

Next Steps