-
-
Notifications
You must be signed in to change notification settings - Fork 653
-
-
Notifications
You must be signed in to change notification settings - Fork 653
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
[analyzer] type guards #5167
Comments
From my experience with GADT this is not gonna be very easy to support in our type system. I don't understand the second example though, how is that different from a constrained type parameter? In fact, how is it different from declaring the function |
The second example is about downcasting, here's what I have in mind regarding structure types: typedef MapItem = {
type:String,
}
typedef House = {
>MapItem,
numRooms:Int,
}
class Main {
static function isHouse(mapItem:MapItem):(mapItem is House) {
return mapItem.type == "house";
}
static function doMapItemStuff(mapItem:MapItem) {
if (isHouse(mapItem)) {
$type(mapItem); // House, because isHouse returned true and its return value defined as `(argName is Type)`
}
}
} |
That reminds me of Rust's typestate: http://pcwalton.github.io/blog/2012/12/26/typestate-is-dead/ |
Regarding user defined type guards, I think it's possible to implement them already, using GADTs, like here: |
It occurred to me that this would be a breaking change in this contrived example: function foo(x:Dynamic) {
if ((x is String)) {
$type(x); // String
x = 1; // Int should be String
}
} |
IIRC, TS remembers the wider type for assignment |
This would have to go through a haxe-evolution with actual design and shit. |
Something from TypeScript to consider:
See TS Spec: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#4.24. That alone could help writing code that is both more concise and performant (on static targets, at least).
However, TS has another nice related feature: user-defined type guards. I.e.:
That would help a lot when working with anonymous structures where reflection is required to determine the actual value type. I think this also plays well with our
Std.is
,Reflect.isFunction
, etc functions. e.g.:The text was updated successfully, but these errors were encountered: