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 != nullnarrowsxto non-null in theifbody -
if x == nullnarrowsxto non-null in theelsebody - the opposite side can narrow to
nullwhen 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.
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.