moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

?= operator (assign if nil)

Open aleclarson opened this issue 7 years ago • 6 comments

This code..

foo ?= {}

-- If the ?. operator is added:
foo?.bar ?= {blue: true}

..would compile to:

local foo
if foo == nil then
  foo = {}
end

if foo ~= nil and foo.bar == nil then
  foo.bar = {blue = true}
end

What do you think, @leafo? Have you considered this feature already?

Cheers!

aleclarson avatar Feb 22 '18 03:02 aleclarson

I edited this thread to remove mention of the ?. operator,
since it has been discussed already: #285 #122

@leafo hasn't directly addressed those issues yet.

...but back in September 2016, he said:

I'm not really interested in adding a new operator

I'm of the opinion that these operators are extremely useful!

aleclarson avatar Feb 22 '18 03:02 aleclarson

do you mind closing this issue as we already have other issues open for this purpose?

RyanSquared avatar Feb 22 '18 15:02 RyanSquared

@RyanSquared There is no issue proposing the ?= operator, but there is this comment mentioning it. So I think there's reason to keep this open.

aleclarson avatar Feb 22 '18 15:02 aleclarson

I don't think it's reasonable to create a new issue for every comment on a post suggesting something. The operators are directly linked and - for me at least - that comment is still in the primary fold when loading the page.

(perhaps I should create a new issue to leave this comment?)

RyanSquared avatar Feb 22 '18 15:02 RyanSquared

@RyanSquared I don't think a slippery slope exists here. Proposals for new operators should get their own dedicated threads for discussing their pros and cons exclusively. I'll edit this thread to focus on
?= since ?. has been discussed. I think |> should have its own thread, too.

aleclarson avatar Feb 22 '18 15:02 aleclarson

The two features are different:

a?.b vs a ? b. CoffeeScript uses the same symbol, but C# for example doesn't. CoffeeScript actually even has a 3rd meaning: a? b, which is x(b) if typeof(x = a) is function.

vendethiel avatar Feb 22 '18 15:02 vendethiel