-
Notifications
You must be signed in to change notification settings - Fork 59
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
Additional syntax for safe navigation #114
Comments
Some more niche concerns: Null-only assignmentWhile I was reading through the Python proposal linked in #89, I found an additional piece of syntax they specify. The
I personally find this syntax to pretty redundant, as unlike with Python, Haxe can represent the expression in a single line. Thus, it would only reduce the readability of code, at least for those who were not aware of the syntax. Wanted to bring it up just in case someone else thinks there's a worthy case to be made for it. Null coalescing overloadingIt was mentioned during the main implementation of safe navigation that overloads of the Thankfully,
I think support for this should be considered in the future, since as stated, the |
Currently
I think |
Note that when I called the syntax awkward, I was referring to the |
For example, Dart also doesn't have |
But |
For deeply nested nullable arrays I would suggest creating an abstract over |
Does this work on, say, arrays provided by JSON? |
Sure. Abstracts are a compile time only feature. Here is an example: class Test {
static function main() {
var x:{?board:NullableArray<NullableArray<Int>>} = haxe.Json.parse("{}");
trace(x?.board[1][2] == null);// true
}
}
abstract NullableArray<T>(Array<T>) {
#if (js && js_es == 6)
@:op([]) inline function get(i:Int):Null<T>
return js.Syntax.code('{0}?.[{1}]', this, i);
#else
@:op([]) function get(i:Int):Null<T>
return
if (this == null) null
else this[i];
#end
} |
I wanted to make a new home for a topic of discussion that is somewhat dispersed right now.
Safe navigation was implemented for object field access in 4.3.0, however it was not implemented for array access or function calls, despite being included in the original proposal #89 .
Array Access
The original proposal dictated that accessing
nullArray?.[0]
would return null, as opposed tonullArray[0]
which currently throws a TypeError.This syntax is notably useful in cases where you are working with nested arrays, like
nullArray?.[i]?.[j]?.[k]
. The fallback behavior is also very clear.The syntax of
?.[]
is used over?[]
since the latter is indistinguishable from a ternary operator by the parser. This syntax would notably be shared with Javascript and no other language.Function Calls
The original proposal dictated that accessing
nullFunction?.()
would return null, as opposed tonullFunction()
which currently throws a TypeError.This syntax is notably useful primarily in the case where you are performing multiple object chaining operations in one line, such as
object1?.object2?.myFunction?.()
. It also may be used in the case where you use an optional callback function.The main reasoning behind these not being implemented with the original proposal, as stated here: https://www.youtube.com/watch?v=lkpoTcHKjSE&t=454s
The text was updated successfully, but these errors were encountered: