Class Declaration
Classes in Morph define objects with fields and methods.
Basic Syntax
Player class {
name is "Unknown" as string;
health is 100 as int;
}
Convention: Omit is for class declarations (it's optional).
With is (Also Valid)
Player is class {
name is "Unknown" as string;
}
Both forms compile identically. Omitting is is the recommended style.
One Class Per File
Morph enforces a strict one-class-per-file rule. The class name must match the filename:
| File | Class Name |
|---|---|
Player.mx | Player class { } |
Vector2.mx | Vector2 class { } |
Main.mx | Contains Init method (no class required) |
This is enforced by the compiler when max_classes_per_file = 1 in .morphsettings.
Creating Instances
Call the class name as a function:
p is Player();
Print(p.name); // "Unknown"
Print(p.health); // 100
Access Modifier on Class
Player public class {
// visible outside this module
}
Generic Classes
Classes can accept type parameters:
Box class<T> {
value is T;
}
b is Box<int>();
b.value is 42;
See Generic Classes.
Next Steps
- Fields — Class fields and
this - Constructors — Initialization methods
- Inheritance — Extending classes