You are browsing as a guest. Sign up (or log in) to start making projects!

1h 46m 58s logged

Devlog: Letting Union Narrowing Follow Field Access

Before v1.4.13, union narrowing only really worked when the thing being narrowed had a variable name due to almost every similarity check relying on name_start and name_length.

This was dumb, because semantic unions already knew how to test tags and extract members. Codegen was not the issue, but rather the annoying part was that the typechecker could remember this:

if value is int:
    n: int = value as int
end

but not this:

if box.value is int:
    n: int = box.value as int
end

So I still had to do a little temp-variable ritual:

tmp: Value = box.value
if tmp is int:
    n: int = tmp as int
end

Which was gross. Not necessarily catastrophic, just gross and annoying. I wanted needed convenience

What changed

The compiler was remembering narrowings by variable name, not by expression shape, so box.value looked obvious in source (to me, at least), but to the typechecker it was basically some random expression and not something it could reliably recognize again later.

A variable name already had a stable identity, whereas field access chain did not. So the change was to let narrowable expressions represent more than just AST_VAR_REF.

For now, that only means stable dot-access chains.

The typechecker can record a narrowing for an expression like:

box.value

and then recognize the same expression later when a cast shows up.

So this works directly now:

if box.value is int:
    n: int = box.value as int
end

Same for the other union members, like string.

The detour

My first example accidentally wandered into a different feature:

if maybe_box.value != null:

That exposed a real nullable-field-guard gap, but that is not what this was supposed to be about. Very rude of the test case to have ambition hmph >:(

Where this leaves it

Semantic union narrowing now persists past field access — at least for stable dot-access chains — and the temp-local is no longer needed.

Butttt now I gotta work on nullable-field-guards, ugh (hehe)

0
6

Comments 0

No comments yet. Be the first!