Kipper
Kipper copied to clipboard
[Feature] Implement branch-specific type narrowing given type check conditions (implicit type casting)
Is there an existing proposal for this?
- [X] I have searched the existing issues
This feature does not exist in the latest version
- [X] I am using the latest version
Proposal
Kipper currently does not support any form of implicit type narrowing as the language has until now primarily operated with primitives and only with v0.12.0
has added support for objects, classes and interfaces. To properly support such structures though advanced type mechanisms will be required that allow the user to easily narrow down the type of a dynamic value.
For example, using a matches
expression:
var obj2: obj = { key: "value" };
interface Sample {
key: str;
}
if (obj2 matches Sample) {
print(obj2.key); // -> ERROR
}
While the obj2
was obviously typed checked and was narrowed down to Sample
in the given branch, the compiler has no register for the scope of the if-statement indicating that the type has been altered in any way meaning it throws an error regardless of the type check operation done beforehand.
To fix this the compiler will need a specific type register where narrowed and implicitly cast types should be stored allowing the user to refer to any value in any given branch branch with the correct type.
Exact behaviour / changes you want
- [ ] Implement type register for if-statement and conditional expression branches allowing types to be used in their appropriately narrowed form.
- [ ] Add advanced type evaluation where references are cross-checked with any given register to ensure that the type being used is the correct type.
Related issues
- #496
- #706
- #707