-
Notifications
You must be signed in to change notification settings - Fork 200
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 / null-conditional member access operator #141
Comments
+1 for For C#: this connects a bit with #74 - the safe navigation operator was added in C# 6 which is the newest version (but not super new - mid 2015) - and is supported by all the major .NET implementations (.NET 4.5, Mono, dotnet core). My thoughts is to just use the |
@LogicAndTrick It should be probably not that hard to:
|
Here is a quote from Python's PEP 505 referenced above:
Here are samples for PHP // null -coalescing
$v = $a ?? 'foo'; // if $a is defined and not null, then use value of the $a as the result of the whole expression, otherwise use use the 'foo' as the result of the whole expression.
// null -aware member access
$v = isset($a) ? $a->bar() : null; // if $a is defined and not null, then use result of the $a->bar() as the result of the whole expression, otherwise use null as the result of the whole expression.
// null -aware index access
$v = isset($a) ? $a['bar'] : null; // (string indexes) if $a is defined and not null, then use result of the $a['bar'] as the result of the whole expression, otherwise use null as the result of the whole expression.
$v = isset($a) ? $a[2] : null; // (numeric indexes) if $a is defined and not null, then use result of the $a[2] as the result of the whole expression, otherwise use null as the result of the whole expression. |
Following the discussion in kaitai-io/kaitai_struct_webide#16, there is a proposal to introduce some sort of "safe navigation" or "null-conditional member access" operator into our expression language.
There's a Wikipedia article on this operator.
Actually, there should be probably at least 2 different operators - i.e. safe member access operator (
a.b
wherea
can be null) and safe collection indexing operator (a[x]
wherea
can be null).Known existing implementations in target languages:
?.
and?[]
&.
?.
and?[]
?.
.?
Drafts:
?.
and?.[]
Questions to decide:
?.
, as it seems to be used by majority of existing implementations.Cc @xavrr
The text was updated successfully, but these errors were encountered: