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

Idea: Null/nil coalescing operator: ?. and ?\ #285

Open
refi64 opened this issue Aug 30, 2016 · 8 comments
Open

Idea: Null/nil coalescing operator: ?. and ?\ #285

refi64 opened this issue Aug 30, 2016 · 8 comments

Comments

@refi64
Copy link

refi64 commented Aug 30, 2016

Currently, I often see stuff like this:

some_value\xyz! if some_value
var = if some_value then some_value.attr else nil

Many languages (C#, Dart, many others) now have a null coalescing operator. Adding that to MoonScript would turn the above code into:

some_value?\xyz!
var = some_value?.attr

(Of course, the ? could also be put after the symbol like some_value\?xyz and some_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.

@buckle2000
Copy link

buckle2000 commented Dec 9, 2016

Cound be useful.

@Drestin
Copy link

Drestin commented Jun 30, 2017

That would be great ! I also think of ?? and ?=.

-- a = if b != nil then b else c
a = b ?? c
-- a = b if a == nil
a ?= b

@vendethiel
Copy link

Could also be useful to have anaphoric constructions if x then f that => local that; that = x; if that then f(that).

@RyanSquared
Copy link
Contributor

Would that not be achievable by f(x) if x ?

@vendethiel
Copy link

It's an issue if x has side effects.

@RyanSquared
Copy link
Contributor

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 ? syntax.

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

@vendethiel
Copy link

I really think that those should be two different things, tho I'm not opposed to a ?|> truthy pipe, or something like Elixir's with. See satyr/coco#197.

@OmarAssadi
Copy link

I would love to see this feature added. Coming from the JVM world, it's one of the things I really miss from Kotlin.

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