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 Printable4+
def to_str5+
"Printable"6+
end7+
8+
def print_info9+
puts to_str10+
end11+
end12+
13+
module Measurable14+
def measure15+
4216+
end17+
end18+
19+
class Widget20+
include Printable21+
include Measurable22+
23+
def initialize(name)24+
@name = name25+
end26+
27+
def name28+
@name29+
end30+
31+
def to_str32+
@name33+
end34+
end35+
36+
w = Widget.new("button")37+
puts w.name # button38+
w.print_info # button (uses Widget#to_str, not Printable#to_str)39+
puts w.measure # 42 (from Measurable)40+
41+
class Gadget42+
include Printable43+
44+
def initialize(label)45+
@label = label46+
end47+
48+
def label49+
@label50+
end51+
end52+
53+
g = Gadget.new("dial")54+
g.print_info # Printable (uses Printable#to_str since Gadget doesn't override)src/codegen.cmodifiedView file
Loading diff…
src/codegen.hmodifiedView file
Loading diff…