Walrus/Language
Classes
Basic class declarations, fields, instance methods, statics, and inheritance in Walrus.
Updated April 13, 20262 min readWalrus / Classes
Walrus supports classes with fields, instance methods, static methods, and inheritance.
Declaring a class
Walrus
class Actor:
var hp: int = 100
fn alive() -> bool:
return hp > 0
static fn make() -> Actor:
return Actor { }Inheritance
Walrus
class Enemy : Actor:
override fn tick(dt: float):
hp -= 1Walrus also supports virtual and override on methods where dynamic dispatch matters.
Construction
Walrus uses a unified construction syntax for classes and objects:
Walrus
let actor = Actor { hp = 100 }
let enemy = Enemy { }You can also use property shorthand if a local variable matches the field name:
Walrus
let hp = 150
let player = Actor { hp } # same as { hp = hp }Classes are heap-backed owned values, so they follow the ownership rules described in Memory Ownership.