Skip to main content

Type Casting

Morph uses the as keyword for type conversions, with maybe for safe (nullable) casts and then for chaining multiple conversion steps.


Simple Cast

Convert a value to a different type using as:

value is 10 as int;
value as float; // convert int → float

Nullable Cast with maybe

The maybe modifier makes a cast safe — if the conversion fails, the result is null instead of an error:

value is "hello";
value as maybe int; // returns null if can't parse as int

Without maybe, a failed cast produces a runtime error. With maybe, it fails gracefully.


Cast Pipeline with then

Use then to chain multiple conversion steps in sequence:

value is 10 as int;
value as maybe dynamic then string then float;

This performs a cast pipeline:

  1. Cast value to dynamic (nullable — maybe applies to the whole pipeline)
  2. Then convert to string
  3. Then convert to float

Each step feeds into the next.


Parenthesized Cast Steps

You can parenthesize individual steps for explicit nullable control:

value as (maybe int);        // nullable cast to int
value as (maybe float); // nullable cast to float

Pipeline Syntax Summary

SyntaxMeaning
x as intCast x to int (error on failure)
x as maybe intCast x to int (null on failure)
x as int then stringCast to int, then to string
x as maybe int then floatNullable pipeline: intfloat
x as (maybe int)Parenthesized nullable single cast

Real Example

From CSharpFeatures.mx:

value is 10 as int;
value as maybe dynamic then string then float;

Next Steps