?= operator (assign if nil)
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!
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!
do you mind closing this issue as we already have other issues open for this purpose?
@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.
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 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.
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.