heap-aliasing-ternary-alias-write.ts 561 B

12345678910
  1. // Conditional alias: `a = flag ? box1 : box2` — at runtime `a` IS one of the two
  2. // boxes, so `a.f = secret` mutates box1.f (flag true) or box2.f (flag false); the
  3. // reader ask interpolates BOTH, so on every execution ask#1's output reaches ask#2.
  4. // Expected: ask#1 -> ask#2 data; ask#2 -> sink.
  5. const box1: { f: string } = { f: "" };
  6. const box2: { f: string } = { f: "" };
  7. const flag = Math.random() > 0.5;
  8. const a = flag ? box1 : box2;
  9. a.f = await agent("writer").ask<string>("secret");
  10. return agent("reader").ask<string>(`use ${box1.f} and ${box2.f}`);