Commit 30f6a5ce

30f6a5cec650Merged

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

Description

feat: sp_RbValue Phase 2 — heterogeneous arrays and bimorphic duck typing

Phase 2a: Array literals now generate sp_RbArray (heterogeneous), with sp_RbValue elements. arr.each iterates with POLY-typed block params, and arr.length returns the count. All array literals use sp_RbArray; sp_IntArray remains for Range#to_a results.

Phase 2b: When a function parameter receives objects of 2+ different classes (e.g., Dog.new and Cat.new), type inference widens to POLY and tracks the known class set. Method calls on such parameters generate bimorphic inline if/else dispatch using per-class tags in the sp_tag enum (SP_TAG_Dog = 64, SP_TAG_Cat = 65, etc.). Objects are boxed via sp_box_obj(tag, ptr) and unboxed with tag check + pointer cast.

Also generates default constructors for classes with no initialize method, so Dog.new / Cat.new work for simple data-less classes.

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

Details

Commit
30f6a5cec650
Parent
ba065596d617

examples/bm_poly2.rbaddedView file

1+# Test Phase 2: heterogeneous arrays, bimorphic dispatch
2+
3+# Heterogeneous array
4+arr = [1, "two", 3.0, true, nil]
5+arr.each do |x|
6+ puts x
7+end
8+# 1, two, 3.0, true, (blank)
9+
10+# Array with mixed types
11+nums = [10, 20, 30]
12+puts nums.length # 3
13+
14+# Duck typing (bimorphic)
15+class Dog
16+ def speak
17+ "Woof!"
18+ end
19+end
20+
21+class Cat
22+ def speak
23+ "Meow!"
24+ end
25+end
26+
27+def make_noise(animal)
28+ puts animal.speak
29+end
30+
31+make_noise(Dog.new) # Woof!
32+make_noise(Cat.new) # Meow!
33+
34+puts "done"