-
Notifications
You must be signed in to change notification settings - Fork 60
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
Safe navigation operator #89
Conversation
I do think this would make for a good addition, also combined with null coalescing (PR #85). For the index and call variants, |
The detailed design fails to design how exactly this would be implemented. Also, there's still the open problem that |
Yes, the detailed design should mention the AST changes at least. I guess the least intrusive change would be an optional flag Also is the |
Perhaps, but this is still something that has to be addressed in the proposal. Anyway, my tentative opinion here is that |
I find it useful for optional callbacks. |
|
Maybe instead of |
@RblSb Then we'll suddenly have code style debates about which variant should be used for array access (like we already have for maps). :/ |
Wow, in retrospect I'm very embarrassed I overlooked this. The same can also be applied to the safe function call. It now makes a lot more sense to me why JS uses
I'll go ahead and move it from "Impact on Existing Code" to the "Detailed Design" with a bit more specificity. Wasn't sure if optional flags were desired when it came to AST?
Will acknowledge in proposal as well. |
Changed the proposed syntax of safe array-access and function call to match better with JavaScript's to help avoid conflict with array literals within ternary conditionals. Also made additional changes to help discuss the modification of `ExprDef` and other clarifications.
It is adding syntax for something which, as a whole, is like quicksands. In the end it may encourage some people using |
|
But that's what I dislike, it's like defining a Maybe monad implemented over null. Why the privilege? |
It's an operator that will be given a default meaning, just like |
Ok I see. |
For me the biggest benefit of a safe navigation operator would be to reduce the boilerplate when using In the following case: class Test {
static var myArray:Null<Array<Int>>;
static function main() {
// without @:nullSafety
myArray.push(1);
// with @:nullSafety(Strict)
if(myArray != null) myArray.push(1);
// with @:nullSafety(StrictThreaded)
final myArrayRef = myArray;
if(myArrayRef != null) {
myArrayRef.push(1);
}
}
} So final classVariableTmp = classVariable;
if(classVariableTmp != null) {
classVariableTmp.member();
} when |
This proposal has been accepted, see https://haxe.org/blog/evolution-meeting-2021/ for details. |
Add safe access operators for nullable values.
Rendered version