Ceylon compiler rejects String-only operations in else branch after `is String` check
0 reputation · 01 Apr 2023, 23:44 UTC
Narrowing not applied in the else branch
I have a Ceylon function whose parameter has the union type String|Integer. My understanding of the language specification is that flow-sensitive typing should narrow the value inside an if (is String val) branch to String, and in the else branch to the complement type (here, Integer). I am trying to rely on that complement narrowing instead of writing an explicit cast or a second is check.
The uncertainty is about exactly how far this flow-based narrowing extends: whether it survives re-assignment of the value, whether it applies when the union has more than two members (for example String|Integer|Float, where the else branch should narrow to Integer|Float), and whether behavior differs between the 1.x compiler releases, since I have seen hints that inference and narrowing details changed across minor versions.
Given the project is no longer actively maintained, I also want to confirm whether any unexpected narrowing behavior I observe should be treated as a permanent compiler characteristic rather than something fixable upstream.
- Does the Ceylon specification guarantee complement narrowing in the
elsebranch for multi-member unions, and does it hold after the variable is reassigned? - Are there known differences between 1.2 and 1.3.x in how
is/exists/nonemptynarrowing is applied? - What is the most reliable way to verify which narrowing rules my installed compiler version actually implements?