Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Implement branch-specific type narrowing given type check conditions (implicit type casting) #705

Open
2 of 4 tasks
Luna-Klatzer opened this issue Sep 25, 2024 · 0 comments
Assignees
Labels
feature New feature or enhancement
Milestone

Comments

@Luna-Klatzer
Copy link
Member

Luna-Klatzer commented Sep 25, 2024

Is there an existing proposal for this?

  • I have searched the existing issues

This feature does not exist in the latest version

  • 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or enhancement
Projects
Development

No branches or pull requests

1 participant