Skip to main content

Variable Declaration

Variables in Morph are declared using the is keyword, or optionally without it using bare declaration syntax.


Basic Declaration with is

The is keyword binds a name to a value:

x is 10;
name is "Alice";
pi is 3.14159;
active is true;

The is Keyword Is Optional

In Morph, is is optional. You can declare variables without it:

// With 'is' — recommended for variables
x is 10;
name is "hello";

// Without 'is' — shorthand form
y 20;
count 0;

Convention

ContextRecommendation
Variable declarationUse is — e.g., x is 10;
Variable reassignmentUse is — e.g., x is 20;
Method declarationOmit is — e.g., Run method() { }
Class declarationOmit is — e.g., Player class { }

Both forms compile identically. The convention improves readability.


Bare Declaration

Declaring a variable name alone (without a value) creates an uninitialized dynamic variable:

a;          // dynamic, uninitialized
b 10; // int, value is 10
c as int; // int, uninitialized (typed but no value)
d is 0 as dynamic; // explicit dynamic with initial value

Type Annotation with as

Use as to explicitly specify the type:

x is 10 as int;
name is "hello" as string;
ratio is 0.5 as float;
flag is true as bool;

When as is omitted, the compiler infers the type from the assigned value.


Examples from Real Code

// From Hello.mx
x is 10;
Init is method() {
Print(x);
};

// From IsOptionalDynamic.mx — all valid declarations
a; // bare declaration (dynamic)
b 10; // shorthand (int, value 10)
c as dynamic; // typed declaration (dynamic, no value)
d is 0 as dynamic; // full form

Next Steps

  • Assignment — Reassigning variables and alias semantics
  • Copying — Creating independent copies with another
  • Move — Transferring ownership with move