Skip to content
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

Open
GreyCat opened this issue Mar 29, 2017 · 3 comments
Open

Safe navigation / null-conditional member access operator #141

GreyCat opened this issue Mar 29, 2017 · 3 comments

Comments

@GreyCat
Copy link
Member

GreyCat commented Mar 29, 2017

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 where a can be null) and safe collection indexing operator (a[x] where a can be null).

Known existing implementations in target languages:

Drafts:

Questions to decide:

  1. What symbol shall we use for it? I'd propose to follow ?., as it seems to be used by majority of existing implementations.
  2. Implementations in target languages — what would be exactly the substitute when it's not supported.

Cc @xavrr

@LogicAndTrick
Copy link
Collaborator

+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 ?. operator and target C# 6 as the minimum required version.

@GreyCat
Copy link
Member Author

GreyCat commented Mar 30, 2017

@LogicAndTrick It should be probably not that hard to:

  • add config / CLI option (something like --csharp-version=6) to control it
  • generate version-dependent code based on that option, i.e. workaround with ternary operator

@ghost
Copy link

ghost commented Mar 30, 2017

Here is a quote from Python's PEP 505 referenced above:

The " null -coalescing" operator is a binary operator that returns its left operand if it is not null . Otherwise it returns its right operand.
The " null -aware member access" operator accesses an instance member only if that instance is non- null . Otherwise it returns null . (This is also called a "safe navigation" operator.)
The " null -aware index access" operator accesses an element of a collection only if that collection is non- null . Otherwise it returns null . (This is another type of "safe navigation" operator.)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants