I am wondering what people think of the idea of an actual elvis operator?
Right now we have a null coalescing operator which is nice for optionals. This however does not work with non-optional Bools. The reason I think this would be handy is that you could set a value of a Bool using functions that return Bool but allowing a backup if the first fails while not having to worry about side-effects.
let value = Func1() ? Func1() : Func2() // Potential side-effects and Func1 is called twice. (What if Func1 incremented a value somewhere).
This can be fixed with (of course)
let func1Result = Func1()
let value = func1Result ? func1Result : Func2()
But this kinda defeats the purpose of a ternary operator IMO and the code below is a lot nicer.
let value = Func1() ?: Func2()
Now I did see this thread. Replace ternary _ ? _ : _ operator with a binary _ ? _ operator
It seems they want to remove the ternary operator completely because it uses a “?” it seems and that there is a replacement using an operator overload. The conversation there also got to adding an operator that seems to return an optional if the value is false. I think that is a slightly different issue and I would love to keep the current null coalescing and the ternary operators as is.