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

lazy coalesce syntax proposal #26303

Open
StefanKarpinski opened this issue Mar 2, 2018 · 8 comments
Open

lazy coalesce syntax proposal #26303

StefanKarpinski opened this issue Mar 2, 2018 · 8 comments

Comments

@StefanKarpinski
Copy link
Member

I propose the following syntaxes:

  • val ?? default for val === nothing ? val : default
  • val ?= default for val === nothing ? val : (val = default)
  • d[key] ?? default for get(()->default, d, key)
  • d[key] ?= default for get!(()->default, d, key)
@nalimilan
Copy link
Member

+1, but I think there are two secondary questions to address:

  • should missing be supported too (coalesce handles both nothing and missing)
  • should Some values be unwrapped (coalesce does, and that's what languages with a Nullable type do)

While we're at it, I'll mention that some languages like Kotlin use val!! to assert that val !== nothing, equivalent to the unexported notnothing helper (can be used to unwrap Some too); Swift and TypeScript use a single ! for that, but obviously it's not possible in Julia. Some languages like Swift and Kotlin also allow val?.field as a shorthand for val === nothing ? nothing : val.field, and col?[i] for col === nothing ? nothing : col[i]. We might want to ensure this syntax is available in case we want to support it at some point.

@StefanKarpinski
Copy link
Member Author

We could potentially make prefix !!x an operator. This is a useless thing to do currently since !x is an error unless x is one of true, false or missing and !!x is the identity on all of those values. In order to make that non-breaking, we might want to make it an error now (or at least document that it might change). Of course, if we make !!x mean x === nothing ? error() : nothing then that would actually not be a breaking change since any code that worked before would still work.

@rfourquet
Copy link
Member

Isn't there a typo in the OP with === instead of !==, and also would it be x === nothing ? error() : x for x!! ?

@StefanKarpinski
Copy link
Member Author

Yes, I got the order wrong in the ternary.

@bkamins
Copy link
Member

bkamins commented May 1, 2018

I have just found this issue. My opinion is that if we implement ?? (which I support) we then can update coalesce then to avoid issues described in #26927.

@dpinol
Copy link

dpinol commented Oct 21, 2021

is there any library with a lazy "something" macro?

@quinnj
Copy link
Member

quinnj commented Oct 21, 2021

I'm pretty sure Base has @something now, right @omus ?

@dpinol
Copy link

dpinol commented Oct 21, 2021

Yes, it's even exported thanks!

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

No branches or pull requests

6 participants