-
Notifications
You must be signed in to change notification settings - Fork 191
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
Idea: Null/nil coalescing operator: ?. and ?\ #285
Comments
Cound be useful. |
That would be great ! I also think of -- a = if b != nil then b else c
a = b ?? c
-- a = b if a == nil
a ?= b |
Could also be useful to have anaphoric constructions |
Would that not be achievable by |
It's an issue if x has side effects. |
Something I think would be neat instead would be: x? |> f(_) I feel like this, first off, fits with MoonScript's usage of _ as a placeholder variable (so that you can assign to the placeholder for the "check" portion) as well as being a shorter format, still using the It would compile to something like: a[b]? |> print("Hello, #{_}") turns into local _ = a[b]
if _ then
print("Hello, " .. _)
end This could perhaps also be used as a "function call" selector, so that: *a\b!? |> print(table.concat(_)) turns into local _ = {a:b()}
if _[1] then
print(table.concat(_))
end This would be useful, for example, with IO operations. Take note that ~ could be used to negate it. *file\read!? ~|> print("File read error:", _[2]) local _ = {file:read()}
if not _[1] then
print("File read error:", _[2])
end |
I really think that those should be two different things, tho I'm not opposed to a |
I would love to see this feature added. Coming from the JVM world, it's one of the things I really miss from Kotlin. |
Currently, I often see stuff like this:
Many languages (C#, Dart, many others) now have a null coalescing operator. Adding that to MoonScript would turn the above code into:
(Of course, the
?
could also be put after the symbol likesome_value\?xyz
andsome_value.?attr
, though I find that a lot harder to read.)I think this would especially make a great addition since
nil
is such an important value in Lua.The text was updated successfully, but these errors were encountered: