traits + Sized(): default methods and the sized marker

- `trait` keyword: like `interface`, but tagged `__trait`; its methods
  carry default bodies.  A class that `implements` a trait has those
  bodies mixed in (a class's own method wins), then is checked
  structurally -- so the mix-in runs BEFORE `__implements_check`.
- runtime `__trait_mixin(cls, trait)`: copies a trait's default methods
  into the class (skipping `__*` meta keys and existing members).
- `Sized` -- the marker object for "layout known at compile time"; every
  concrete value satisfies it, so `x is Sized` / `implements(x, Sized)`
  hold and signatures can name it (`buf : Sized()`).
- tests/traits.cat + suite: default method, override, `is`, and Sized.
  Suite 144 -> 151.
This commit is contained in:
Minus committed 2026-11-18 17:18:58 +00:00
1 parent 95f3823acf
commit 70728093ef
6 files changed
+133 -9

No files matched your search

+9
View File
@@ -254,6 +254,15 @@ esac
# ---- self-hosted front-end (written in catscript) -------------------
out=`$BIN tests/traits.cat`
check "trait-default" "hello, Marcel" "`echo "$out" | sed -n 1p`"
check "trait-second" "HELLO, Marcel" "`echo "$out" | sed -n 2p`"
check "trait-is" "person is Greeter: true" "`echo "$out" | sed -n 3p`"
check "trait-override" "beep R2" "`echo "$out" | sed -n 4p`"
check "trait-robot" "robot is Greeter: true" "`echo "$out" | sed -n 6p`"
check "trait-sized" "sized: true" "`echo "$out" | sed -n 7p`"
check "trait-sized-fn" "implements Sized: true" "`echo "$out" | sed -n 8p`"
out=`$BIN tests/comptime.cat`
check "comptime-expr" "ct-expr: 42" "`echo "$out" | sed -n 1p`"
check "comptime-text" "ct-text: hi there" "`echo "$out" | sed -n 2p`"