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

24m 57s logged

Devlog: Teaching Flower to Understand Guard Clauses

gave Flower branch-local inference.

Last update, I added support for branch-local inference, so the compiler could understand things like:

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

and:

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

That was an improvement, but it still meant narrowing stopped at the end of the branch. That meant stuff like returninging in some conditions still did not really work as naturally as it should:

if maybe == null:
    return false
end

real: @int = maybe

or:

if value is string:
    return false
end

n: int = value as int

In v1.4.9 I fixed that.

What changed

Now narrowing propagates past very obvious guard clauses. If one branch definitely returns and the other falls through, the compiler now carries the interpretation forward after the if.

This means that the compiler can now understand:

  • after if x == null: return ..., x is non-null
  • after if x is string: return ..., a two-member semantic union can narrow to its remaining member

I kept this intentionally conservative for now. The compiler is not doing full control-flow analysis yet. It is only recognizing the simplest and most explicit early-exit shape.

How it works

The main addition was a small semantic check for “does this branch definitely return?”

Right now that means:

  • direct return
  • if where both branches definitely return

Once the compiler knows one side exits and the other survives, it applies the branch’s narrowing to the following statements.

Yea.. that’s it lol.

0
8

Comments 0

No comments yet. Be the first!