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 distance3+
(x * x + y * y)4+
end5+
6+
def to_s7+
"(#{x}, #{y})"8+
end9+
end10+
11+
p1 = Point.new(x: 3, y: 4)12+
puts p1.x # 313+
puts p1.y # 414+
puts p1.distance # 2515+
p1.x = 1016+
puts p1.x # 1017+
18+
class Entry < Struct.new(:name, :value, keyword_init: true)19+
def display20+
"#{name}=#{value}"21+
end22+
end23+
24+
e = Entry.new(name: "foo", value: 42)25+
puts e.name # foo26+
puts e.value # 4227+
28+
puts "done"src/codegen.cmodifiedView file
Loading diff…