Commit 1d0e0bee

1d0e0bee3e3bMerged

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

Description

Support class X < Struct.new(:fields) pattern in AOT compiler

Detect when a class inherits from Struct.new(...) and generate struct fields, initialize, getters, and setters — same as the existing ClassName = Struct.new(...) pattern but allowing the class body to define additional methods.

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

Details

Commit
1d0e0bee3e3b
Parent
040204822b6a

examples/bm_struct_inherit.rbaddedView file

1+class Point < Struct.new(:x, :y, keyword_init: true)
2+ def distance
3+ (x * x + y * y)
4+ end
5+
6+ def to_s
7+ "(#{x}, #{y})"
8+ end
9+end
10+
11+p1 = Point.new(x: 3, y: 4)
12+puts p1.x # 3
13+puts p1.y # 4
14+puts p1.distance # 25
15+p1.x = 10
16+puts p1.x # 10
17+
18+class Entry < Struct.new(:name, :value, keyword_init: true)
19+ def display
20+ "#{name}=#{value}"
21+ end
22+end
23+
24+e = Entry.new(name: "foo", value: 42)
25+puts e.name # foo
26+puts e.value # 42
27+
28+puts "done"