-
-
Notifications
You must be signed in to change notification settings - Fork 654
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
Pattern matching with null leads to runtime error #2580
Comments
I'm not sure if this isn't working as expected. You can add a
We don't want to add hidden null checks everywhere, so it seems fair to leave this responsibility to the user as with all other null scenarios. What do you think? |
I agree we shouldn't add null checks when the user hasn't been explicitly saying it can be Null (or optional). But when that's the case we should add null checks to prevent an error occurring on pattern matching, which is unexpected. |
i actually run into this problem while writing the python backend, see https://github.com/frabbit/hx2python/blob/development/src/python/gen/PythonTransformer.hx#L384. Actually i would prefer null checks if you deal with |
@ncannasse proposal seems fair. 2014-01-29 frabbit [email protected]
David Elahee |
I don't know, this could potentially add many null checks in places where they aren't required. Consider code like this:
This is good practice, but adding implicit null checks would cause this to be detrimental to performance. We cannot properly check the redundancy of the null check without some notion of static analysis. I also don't quite see why pattern matching should be special with regards to null checks. We don't add them upon |
i guess it's only required for nested parameters (inside of patterns) not at the top level of the switch. |
Agreed with @frabbit |
This is a bit messy to implement, I'll move it to 3.2. |
We cannot add this without adding null as a required value for exhaustiveness. Consider this:
There is no case which covers I don't remember why null patterns are not part of the exhaustiveness check, but the relevant code is commented out so I assume there was some reason. |
I have added this. We will have to deal with some pseudo-regressions now such as HaxeFoundation/format#13 |
i have to say, i quite like the required toplevel check against null, because it's a source of bugs. |
My take is that if you remember to type nullable types as |
yes, it's good if |
you added an implicit null case, which means that this example compiles fine (no exhaustiveness check). Is that expected? class Test {
public static function main() {
var o:Null<Int> = 0;
var x = switch o {
case x if (x > 1): x;
}
trace(x); // null
}
} |
I don't think that's related to this change. It should give an error regardless. |
ok, i added an issue #2914 |
The following code fails at runtime with:
Uncaught exception - Invalid field access : index
The text was updated successfully, but these errors were encountered: