You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Class inheritance: extends Parent + chained method dispatch
Sixth piece of the Python-ergonomics catch-up. Extends the class
system from the previous commit with single inheritance via the
`extends` keyword. Method dispatch walks the parent chain when the
child class doesn't define the method.
Syntax:
class Animal {
name;
fn greet(self) { return concat_many(\"hi from \", dict_get(self, \"name\")); }
fn species(self) { return \"generic animal\"; }
}
class Dog extends Animal {
name;
breed;
fn species(self) { # override
return concat_many(\"dog (\", dict_get(self, \"breed\"), \")\");
}
fn bark(self) { return concat_many(dict_get(self, \"name\"), \" says woof\"); }
}
class Puppy extends Dog { ... } # multi-level chains work
h d = Dog(\"rex\", \"lab\");
d.greet() # inherited from Animal
d.species() # overridden in Dog → \"dog (lab)\"
d.bark() # Dog's own method
Implementation:
AST:
Statement::ClassDef gains `parent: Option<String>` field.
Parser:
Token::Extends keyword; parse_class_def reads optional
`extends Parent` clause between name and the LBrace.
Interpreter:
- New `class_parents: HashMap<String, String>` table on the
Interpreter, populated by execute_stmt when ClassDef runs.
- Method dispatch in call_function walks the parent chain:
try `<Child>__<method>`, then `<Parent>__<method>`, etc.,
bounded to 64 hops as a cycle-safety net.
- Chain walk happens BEFORE module-qualified dispatch so
instance method lookup still wins over dotted module calls.
Formatter:
Prints `class Name extends Parent { ... }` when parent is set.
Tests (examples/tests/test_classes.omc — 4 new, all pass):
- test_inherited_method: Dog inherits Animal.greet without redefining
- test_override: Dog.species overrides Animal.species
- test_own_method: Dog.bark not on Animal
- test_multi_level_inheritance: Puppy chains 2 hops up to Animal.greet,
1 hop up to Dog.bark, overrides both at species
What's NOT yet:
- super() calls. Method overrides currently can't invoke the parent's
implementation explicitly. Could be sugar for the mangled parent
name lookup; deferred to a later session.
- Multiple inheritance / mixins / MRO. Single-parent chain only.
- Class methods / static methods. All methods are instance methods.
- Construction calls parent's constructor automatically. Right now
the child controls its own fields; no auto-init from parent.
Regression: 11 class tests (was 7 + 4 new) + 8 exception + 10 fstring
+ 10 regex + 17 JSON + 57 substrate + 70 builtins + 18 harmonic libs
+ 16 heal = 217 OMC tests, all pass.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
0 commit comments