Skip to main content

Dynamic Type

The dynamic keyword declares a variable with a runtime-determined type. Dynamic variables can hold any value and their type can change during execution.


Declaring Dynamic Variables

// Explicit dynamic
x is 0 as dynamic;
y as dynamic;

// Bare declaration — implicitly dynamic
a; // dynamic, uninitialized

A bare declaration (just an identifier followed by a semicolon) automatically becomes a dynamic variable.


Dynamic Behavior

Dynamic variables accept any value type:

value as dynamic;
value is 42; // holds an int
value is "hello"; // now holds a string
value is true; // now holds a bool

Real Example

From IsOptionalDynamic.mx:

RunFmaBench method() as int {
a; // dynamic, bare declaration
b 10; // int shorthand
c as dynamic; // explicit dynamic
d is 0 as dynamic; // dynamic with initial value

Print("is-optional + dynamic demo");
return 0;
}

When to Use dynamic

Use CaseRecommendation
Type is known at compile timeUse a concrete type (int, string)
Type varies at runtimeUse dynamic
Interfacing with untyped dataUse dynamic
PrototypingUse dynamic, refine later

Next Steps

  • Type Casting — Converting between types with as, maybe, and then