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

Add a Conditional Access Operator (Null Conditional Operator) #146

Open
9 tasks
kindlich opened this issue Jun 2, 2024 · 0 comments
Open
9 tasks

Add a Conditional Access Operator (Null Conditional Operator) #146

kindlich opened this issue Jun 2, 2024 · 0 comments
Labels
enhancement New feature or request parser verifier
Milestone

Comments

@kindlich
Copy link
Member

kindlich commented Jun 2, 2024

In order to make working with optional types easier without losing the information that we are using optionals, we want to introduce a Conditional Access Operator.

Null conditional operator: (x?.field or x?.method(...))

  • Used to call a method or load a field on an optional value
  • Can be used only on optional values (T?), usage on other types is an error
  • Always returns an optional - null if the value if null, the result of the method call / field otherwise
  • If the resulting value is optional, it is flattened - there are no doubly optionals
  • Not supported for operators, including [ ] and ( )
  • No short-circuit, if the operand before the ?. is null, any chained calls are stil evaluated.
    • a?.getB().c would error unless B? had a member c
  • Uses the members from the base-type (T if invoked on T?)

Acceptance Criteria:

  • Decide: Should this be usable on member-get and member-set operator => Requires: Member getter / setter in zc #144 ; If that issue has been completed by then, implement if we decide we want this, otherwise create new issue to track that implementation.
  • Test Cases:
    • Simple test case with null- and non-null-value

    • ?. flattens optional return type

    • "Collision:" Member exists on both Nullable-type and on base-type, which one does the null-safe operator use?

      public expand string? { public const get internalized as string => this == null ? "NULL" : this.toUpperCase(); }
      public expand string  { public const get internalized as string => this; }
      
      var x as string? = null;
      println(x?.internalized); // null
      println(x.internalized); // "NULL"
      
      var y as string? = "Hello";
      println(y?.internalized); // "Hello"
      println(y.internalized); // "HELLO"
      
    • Properly converts back and forth between null and -1 for null as usize

    • If decision on member-get and member-set operator: Tests for that

    • Fails for non-optional type

    • Operator cannot be overridden in a custom type

@kindlich kindlich added enhancement New feature or request verifier parser labels Jun 2, 2024
@kindlich kindlich added this to the v2.0.0 milestone Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request parser verifier
Projects
None yet
Development

No branches or pull requests

1 participant