-
Notifications
You must be signed in to change notification settings - Fork 34
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
narrow type in else block of if (is...) #74
Comments
Don't believe this guy, I never said any such thing! ;) |
So how would
In any case, the behavior in the two examples should be consistent. |
You could write:
|
Obviously. I just wanted to point out that the question posed by this ticket applies to |
OK, sure. |
This feature would be even more useful for the |
As mentioned in #170, I could now start work on this. |
I got a remark during my presentation that people expect this to be true:
|
@FroMage Yeah so I will work on it as one of the first things for M5, I suppose. |
I wonder if we need the same thing for the |
Sounds logical to me. |
+1 for ^4 (#74 (comment)) |
Note to self: Once we implement this feature, we should also add |
Note that #170 proposed that flow-dependent typing should work together with definite return analysis, to allow the following:
This would be much more difficult to implement. |
I think it should work together with some more general type of flow analysis, because this should totally work without an String? str = "abc";
print(str.uppercased); // method or attribute does not exist: uppercased in type String? (In a real-world scenario, A more brutal scenario for that analysis would be: Integer i = ...;
String? str;
if (i == 42) {
str = "abc";
}
...
if (i == 42) {
// since i isn’t variable, this block is entered iff the first if block is entered
print(str.uppercased);
} (Not sure how useful that would be) |
@lucaswerkmeister see #536, which is planned for Ceylon 1.2. |
... I really should search before making comments. Thanks! |
I have implemented this in the typechecker for |
Well, no, these are totally different things. In other "useless" narrowings the entire |
Ok, understood. |
@gavinking how can I actually obtain the type of the |
The |
I think something else changed because a totally unrelated test fails when I typecheck with this branch... I get a weird error when refining methods in a subtype like this: span = whatever.span;
measure = whatever.measure; I had to change it to shared actual List<[Integer+]> span(Integer from, Integer to) => whatever.span(from, to);
shared actual List<[Integer+]> measure(Integer a,Integer b) => whatever.measure(a,b); |
@chochos Please give me a simplified bit of code that reproduces the error. |
JS backend is done: |
@gavinking Something as simple as this won't compile with this typechecker: class Bug(String s) {
equals=s.equals;
} But it's only with methods; attributes don't have this problem: class Bug(String s) {
hash=s.hash;
} |
|
F**k, forget it, all that branch switching has liquified my brain. |
@gavinking I can confirm @chochos ' problem with this branch, have had time to take a look at that? |
It isn't this branch. It's on main. And I already have a partial fix for it. Sent from my iPhone
|
Ok! Thx |
It's fixed by @7e9103f60fa456aed7e9c7bc028674ed6754f39b. |
Not really. I'll open a separate issue. |
@gavinking the branch doesn't apply cleanly to master, could you merge master again please? |
@quintesse done. |
When the type can be narrowed in an else clause (of an if or switch statement), the grammar adds a synthetic variable to the else clause, which includes the identifier of the value being switched on. visitChildren() found this identifier, causing an unexpected token to be written.
Mostly just removing checks, but in one case I didn’t like how many lines there were between the if and the else, so I turned it into a switch instead so that you can see the type of the variable in the other block as well.
Merged. Closing, finally! |
In 9 days time, this issue would have turned three years old :-/ |
@gavinking I stumbled upon something minor that I think is related to this issue : String foo<Bar>
(String|Bar element){
if (is String element){
return element;
}else{
return element;
}
}
|
@sgalles OK, I agree that this was worth improving. Not a bug precisely, but anyway I have improved the algorithm in |
Oh, cool, this also addresses #1238! |
Tako gives this example:
He argues, rather reasonably, that the compiler should be able to reason that
x
has typeInteger
where the error occurs.I agree that it would be possible. This would not amount to supporting types like
X~Y
, which Ross says results in undecidability, if I recall correctly. It just means being able to utilize the identityX|Y~Y == X
, which we already do utilize in certain places.On the other hand:
if (is Integer x)
orif (exists x)
.switch
statement.So I'm not sure that this feature would really be especially practically useful. OTOH, it's easily doable.
The text was updated successfully, but these errors were encountered: