evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.09k stars 125 forks source link

Introduce new operator for syntactic sugar for is None and is not None #368

Closed ArneBachmann closed 6 years ago

ArneBachmann commented 6 years ago

I quite often use None as a fallback/uninitialized value, e.g. for my:str? = None.

To test it, I have to write if my is None or if my is not None. I can use the ?? operator in some situations, but if an expression depends on the value being None or not, it doesn't: result = a() if my is None else b(my)

What about introducing a is None operator? result = a() if ?= my else b(my) or if ?~ my: result = b(my); else result = a()

That would be an unary operator similar to not, but checking for the object being None or not.

ArneBachmann commented 6 years ago

Oh, BTW, the simple check if my: <block> only works if my is not one of 0, False, "", [], s{}, f{} or {}

ymeine commented 6 years ago

Exactly like the existence operator in LiveScript?

ArneBachmann commented 6 years ago

Hey @ymeine, LiveScript is new to me, and it's explained in terms of JavaScript, which I don't know. But I believe that the ? suffix-operator is not what I'm looking for, because it either evaluates to null or the obect's value, which is exactly what Coconut's ?? does already.

ymeine commented 6 years ago

Sorry, I should have explained too.

This existence operator in LiveScript evaluates to a boolean, and returns true if the value is not void, false otherwise (by void I mean null or undefined, which are equivalent to Python's None).

However, beyond this unary operator, there is a binary equivalent using the same character, and doing the equivalent of what ?? is doing in Coconut.

Why I point that out is because I like the fact that LiveScript made no distinction between the unary and the binary operator regarding the choice of the character, since in both cases it uses the existence of the first operand to choose the output value of the operation.

a? # => false
a ? 10 # => 10
a = 5
a? # => true
a ? 10 # => 5
ArneBachmann commented 6 years ago

Thanks for the explanation. This would work then:

a(may) if may? else b()

evhub commented 6 years ago

What's wrong with obj is None? It's short, descriptive, and gets the job done. This feels to me like the one None-aware operator use case where Python really already has this down.

ymeine commented 6 years ago

@ArneBachmann LiveScript doesn't have this ternary if form (unfortunately) but logically it would work (like this: if may? => a(may) else b() or in more idiomatic way: if may? => a that else b!)

@evhub For even more conciseness but also consistency. And contrary to a classic boolean or operator, there is no ambiguity here, it just checks None (while in LiveScript for instance there was still a choice to consider both null and undefined).

evhub commented 6 years ago

@ymeine If you need to use boolean or with Nones, just use the ?? operator. I don't think this adds anything of value on top of the combination of ?? and obj is None.

ymeine commented 6 years ago

@evhub I am not the requester of the feature. But that's true I do think it makes sense to have the unary equivalent of the binary operator ??. The mention of the "or-like feature" in LiveScript was just to explain how the operator works there, highlighting that it's using the same special characters for both unary and binary versions. I'm aware the binary version already works in Coconut, and that's actually the unary equivalent which is being requested.

Therefore yes, the two benefits are only:

Of course this is already doable is a non-ambiguous and non-cumbersome way, compared to what the binary version solved (a if a is not None else b becomes a ?? b which is far more valuable, especially when starting to add more operands in the expression)

ArneBachmann commented 6 years ago

Yeah, I second that suggestion regarding definition of ??, although I'm not sure anymore what my original intent was and what situation it came from... If it pops up again I can show a concrete example.