Commit 285ceacf

285ceacf79cfMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Poly Tier 2c: method dispatch on boxed values

A no-arg method call on a poly receiver switches on the boxed object's cls_id and calls the matching class's method (chain-walked for inherited methods), unboxing the pointer; return type unifies over all defining classes. length/size/count also dispatch to the built-in array families (Int/Str/Float/Sym/Poly) so a poly that holds any array kind works.

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

Details

Commit
285ceacf79cf
Parent
e245f491ee75

src/analyze.cmodifiedView file

@@ -176,6 +176,14 @@
176176 if (!strcmp(name, "to_s") || !strcmp(name, "inspect")) return TY_STRING;
177177 if (!strcmp(name, "to_i")) return TY_INT;
178178 if (!strcmp(name, "to_f")) return TY_FLOAT;
179+ /* poly method dispatch: unify the return type over every class that
180+ defines `name` (the runtime cls_id picks the impl). */
181+ TyKind r = TY_UNKNOWN; int found = 0;
182+ for (int k = 0; k < c->nclasses; k++) {
183+ int mi = comp_method_in_chain(c, k, name, NULL);
184+ if (mi >= 0) { r = found ? ty_unify(r, c->scopes[mi].ret) : c->scopes[mi].ret; found = 1; }
185+ }
186+ if (found) return r;
179187 }
180188 }
181189