Commit 464f985e

464f985efb52Merged

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

Description

Add StrPolyHash (string keys, mixed values) and hash fetch-with-block

  • TY_STR_POLY_HASH for string-keyed hashes whose values are mixed/poly; a str-keyed hash that sees >1 value type widens to StrPoly. Values are boxed on literal/[]= ; keys returns a StrArray.
  • h.fetch(key) { default }: has_key? ? get : block-default, the block emitted as a statement-expression so multi-statement defaults work.

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

Details

Commit
464f985efb52
Parent
f457299918a0

src/analyze.cmodifiedView file

@@ -401,7 +401,10 @@
401401 }
402402 /* symbol keys -> SymPolyHash (boxed values), regardless of value type */
403403 if (kt == TY_SYMBOL) return TY_SYM_POLY_HASH;
404- return ty_hash_of(kt, vt);
404+ TyKind hv = ty_hash_of(kt, vt);
405+ /* string keys with a mixed/unsupported value type -> StrPolyHash */
406+ if (hv == TY_UNKNOWN && kt == TY_STRING && vt != TY_UNKNOWN) return TY_STR_POLY_HASH;
407+ return hv;
405408 }
406409 if (!strcmp(ty, "YieldNode")) {
407410 /* value of yield = the block body's value at a call site of this method */
@@ -938,10 +941,15 @@
938941 lv->type = ty_unify(lv->type, ty_array_of(vt));
939942 }
940943 else if (kt == TY_STRING) {
944+ if (vt == TY_UNKNOWN) continue;
941945 TyKind hv = ty_hash_of(TY_STRING, vt);
942- if (hv == TY_UNKNOWN) continue;
946+ if (hv == TY_UNKNOWN) hv = TY_STR_POLY_HASH; /* mixed values */
943947 if (lv->type != TY_UNKNOWN && !ty_is_hash(lv->type)) continue;
944- lv->type = ty_unify(lv->type, hv);
948+ /* a str-keyed hash that has seen >1 value type widens to StrPoly */
949+ if (lv->type != TY_UNKNOWN && lv->type != hv &&
950+ (lv->type == TY_STR_INT_HASH || lv->type == TY_STR_STR_HASH || lv->type == TY_STR_POLY_HASH))
951+ hv = TY_STR_POLY_HASH;
952+ lv->type = hv;
945953 }
946954 else if (kt == TY_SYMBOL) {
947955 /* symbol key -> SymPolyHash (boxed values) */