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

Null Safety / Null Coalescing plans? #77

Open
gnat opened this issue Jan 10, 2024 · 1 comment
Open

Null Safety / Null Coalescing plans? #77

gnat opened this issue Jan 10, 2024 · 1 comment

Comments

@gnat
Copy link

gnat commented Jan 10, 2024

Really like what I'm seeing so far with cyber. Thank you!

Congrats on the big 0.4 release.

Any plans for implementing an ergonomic null safe operator (or safe navigation operator or equivalent)? Use case:

my test = [ 'hello':'world', 'blah':'blah']
my output = ''
output = try "$(test.hello)"
print output -- world
output = try "$(test.hello.missing)"
print output -- none ... Awesome! Easy.
output = try "$(test.hello.missing.goodbye)"
print output -- panic: Field not found in value.

Would be great to see Cyber handling missing nested items in maps with try/catch or ideally implement question mark operators (?? and/or ?. etc)

Why bother? https://www.youtube.com/watch?v=XIhQYRNBAYs

Related: https://en.wikipedia.org/wiki/Null_coalescing_operator
Related: https://en.wikipedia.org/wiki/Safe_navigation_operator

Proposal for Moonscript, which shares a similar syntax with Cyber: leafo/moonscript#285

@fubark
Copy link
Owner

fubark commented Jan 10, 2024

Thanks for the references. Here's what I'm currently thinking for optionals:

Option declaration:

var a ?Foo = none
var a = ?Foo.none
var a ?Foo = [Foo:]
var a = some([Foo:])

Get value or panic: a!
For optional chaining, returns last access or none: a?.b?.c
Chain but fail at first none encounter: a!.b!.c
If a is dynamic it can just be a.b.c

For null coalescing:

var res = a else 123
var res = a else:
    break 123

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

2 participants