From a7eceb21faf04762379f2ce4d23d21bbc8c11929 Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Sun, 18 Jan 2026 12:46:05 +0000 Subject: Deprecate 'repeat' (superfluous), and add named uses. --- examples/features/composition.zc | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/features/composition.zc (limited to 'examples') diff --git a/examples/features/composition.zc b/examples/features/composition.zc new file mode 100644 index 0000000..883c348 --- /dev/null +++ b/examples/features/composition.zc @@ -0,0 +1,38 @@ + +struct Vector2 { + x: float; + y: float; +} + +// Unnamed use (Mixin) +// Transform "is a" Vector2 (inherits x and y) +struct Transform { + use Vector2; + rotation: float; +} + +// Named use (Composition) +// Rigidbody "has a" velocity and position +struct Rigidbody { + use position: Vector2; + use velocity: Vector2; + mass: float; +} + +fn main() { + // Mixin usage - flattened fields + var t = Transform{ x: 10.0, y: 5.0, rotation: 90.0 }; + println "Transform pos: ({t.x}, {t.y})"; + + // Named usage - nested fields + var rb = Rigidbody{ + position: Vector2{x: 0.0, y: 10.0}, + velocity: Vector2{x: 1.0, y: 0.0}, + mass: 50.0 + }; + + // Access using dot notation + rb.position.x += rb.velocity.x; + + println "Rigidbody pos: ({rb.position.x}, {rb.position.y})"; +} -- cgit v1.2.3