Commit 4b82b7f6

4b82b7f6e94cMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · +1 added · ~2 modified

Description

Add include (mixin) support

Modules with instance methods can be included in classes:

  • include ModuleName detected in class body analysis
  • Module instance methods copied to including class (if not overridden)
  • Mixin methods compiled per-class with the including class's self type
  • Implicit self calls resolve via the including class's method table (Widget#to_str overrides Printable#to_str, Gadget uses Printable's)

Test: examples/bm_mixin.rb — Printable + Measurable modules, Widget (with override) and Gadget (without). Output matches Ruby.

All regressions pass.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
4b82b7f6e94c
Parent
6858360b70df

examples/bm_mixin.rbaddedView file

1+# Test include (mixin)
2+
3+module Printable
4+ def to_str
5+ "Printable"
6+ end
7+
8+ def print_info
9+ puts to_str
10+ end
11+end
12+
13+module Measurable
14+ def measure
15+ 42
16+ end
17+end
18+
19+class Widget
20+ include Printable
21+ include Measurable
22+
23+ def initialize(name)
24+ @name = name
25+ end
26+
27+ def name
28+ @name
29+ end
30+
31+ def to_str
32+ @name
33+ end
34+end
35+
36+w = Widget.new("button")
37+puts w.name # button
38+w.print_info # button (uses Widget#to_str, not Printable#to_str)
39+puts w.measure # 42 (from Measurable)
40+
41+class Gadget
42+ include Printable
43+
44+ def initialize(label)
45+ @label = label
46+ end
47+
48+ def label
49+ @label
50+ end
51+end
52+
53+g = Gadget.new("dial")
54+g.print_info # Printable (uses Printable#to_str since Gadget doesn't override)