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

1h 10m 10s logged

Devlog: Adding Obvious Branch-Local Inference to Flower

Before this, narrowing mostly worked in the obvious “true branch” cases:

if maybe != null:
    real: @int = maybe
end

and:

if value is string:
    s: string = value as string
end

But the else branch was still kind of standing there empty-headed, even when the meaning was extremely obvious. Like, if this happens:

if maybe == null:
    return false
else:
    real: @int = maybe
end

then inside the else, maybe is clearly not null.

So now it doesn’t!!

What changed

Flower now does branch-local narrowing in else for small, explicit cases.

That means these now work:

if maybe == null:
    return false
else:
    real: @int = maybe
end

and:

if value is int:
    return false
else:
    s: string = value as string
end

It is only applying inference when the condition says something directly and the opposite branch has one obvious meaning.

Nullable narrowing

For nullable pointer-like values, Flower now understands both sides of simple null checks:

  • if x != null narrows x to non-null in the if body
  • if x == null narrows x to non-null in the else body
  • the opposite side can narrow to null when that is the only meaning left

Semantic union narrowing

The same idea applies to semantic unions, but a bit more ‘held back.’

Essentially, if Flower sees:

if value is int:

then the if branch narrows to int, like before.

Now, if the union only has one other possible member, the else branch can narrow too. So for:

int | string

the else after if value is int can be treated as string.

This is not full control-flow wizardry, since it doesn’t reason across deep boolean chains, early-returns, or giant branches yet. It is deliberately small and local, because I do not want Flower “helping” so much that I need a detective board to figure out what type something is.

0
7

Comments 0

No comments yet. Be the first!