On 21 September 2015 at 03:35, Mark E. Haase email@example.com wrote:
A) Is coalesce a useful feature? (And what are the use cases?)
There seem to be a few main use cases:
1. Dealing with functions that return a useful value or None to signal "no value". I suspect the right answer here is actually to rewrite the function to not do that in the first place. "Useful value or None" seems like a reasonable example of an anti-pattern in Python.
2. People forgetting a return at the end of the function. In that case, the error, while obscure, is reasonable, and should be fixed by fixing the function, not by working around it in the caller.
3. Using a library (or other function outside your control) that uses the "useful value or None" idiom. You have to make the best of a bad job here, but writing an adapter function that hides the complexity doesn't seem completely unreasonable. Nor does just putting the test inline and accepting that you're dealing with a less than ideal API.
4. Any others? I can't think of anything.
Overall, I don't think coalesce is *that* useful, given that it seems like it'd mainly be used in situations where I'd recommend a more strategic fix to the code.
B) If it is useful, is it important that it short circuits? (Put another way, could a function suffice?)
Short circuiting is important, but to me that simply implies that the "useful value or None" approach is flawed *because* it needs short-circuiting to manage. In lazy languages like Haskell, the Maybe type is reasonable because short-circuiting is a natural consequence of laziness, and so not a problem. In languages like C#, the use of null as a sentinel probably goes back to C usage of NULL (i.e., it may not be a good approach there either, but history and common practice make it common enough that a fix is needed).
C) If it should be an operator, is "??" an ugly spelling?
>>> retries = default ?? cls.DEFAULT
Arbitrary punctuation as operators is not natural in Python, something like this should be a keyword IMO.
D) If it should be an operator, are any keywords more aesthetically pleasing? (I expect zero support for adding a new keyword.)
>>> retries = default else cls.DEFAULT >>> retries = try default or cls.DEFAULT >>> retries = try default else cls.DEFAULT >>> retries = try default, cls.DEFAULT >>> retries = from default or cls.DEFAULT >>> retries = from default else cls.DEFAULT >>> retries = from default, cls.DEFAULT
Reusing existing keywords (specifically, all of the above) looks clumsy and forced to me. I agree that proposals to add a new keyword will probably never get off the ground, but none of the above suggestions look reasonable to me, and I can't think of anything else that does (particularly if you add "must be parseable" as a restriction!)
Overall, I'm -0.5 on a "coalesce" operator. I can't see it having sufficient value, and I can't think of a syntax I'd consider justifying it. But if someone were to come up with a Guido-like blindingly obvious way to spell the operation, I would be fine with that (and may even start using it more often than I think).