Commit fe8f7860

fe8f7860bf11Merged

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

Description

Hash#merge with a conflict-resolution block

merge(other) { |k, v1, v2| } copies the receiver, then inserts each key of other; on a key collision the block (key, this-value, other-value) picks the stored value. Works across hash variants. Block params bind as key/value/value types.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
fe8f7860bf11
Parent
a4fd520fe345

src/analyze.cmodifiedView file

@@ -1889,6 +1889,24 @@
18891889 continue;
18901890 }
18911891
1892+ /* hash.merge(other) { |k, v1, v2| } binds key + both conflicting values */
1893+ if (!strcmp(name, "merge") && ty_is_hash(rt)) {
1894+ Scope *ms = comp_scope_of(c, block);
1895+ LocalVar *kp = scope_local_intern(ms, p0); kp->is_block_param = 1;
1896+ TyKind km = ty_unify(kp->type, ty_hash_key(rt));
1897+ if (km != kp->type) { kp->type = km; changed = 1; }
1898+ const char *mp1 = block_param_name(c, block, 1);
1899+ const char *mp2 = block_param_name(c, block, 2);
1900+ const char *mps[2]; mps[0] = mp1; mps[1] = mp2;
1901+ for (int mi2 = 0; mi2 < 2; mi2++) {
1902+ if (!mps[mi2]) continue;
1903+ LocalVar *vp = scope_local_intern(ms, mps[mi2]); vp->is_block_param = 1;
1904+ TyKind vm = ty_unify(vp->type, ty_hash_val(rt));
1905+ if (vm != vp->type) { vp->type = vm; changed = 1; }
1906+ }
1907+ continue;
1908+ }
1909+
18921910 /* hash.fetch(key) { |k| } binds the looked-up key */
18931911 if (!strcmp(name, "fetch") && ty_is_hash(rt)) {
18941912 Scope *fs = comp_scope_of(c, block);