RapydScript icon indicating copy to clipboard operation
RapydScript copied to clipboard

add existential 'operator'

Open atsepkov opened this issue 10 years ago • 8 comments

CoffeeScript has the concept of existential operator: a?, which compiles to the following:

typeof a !== "undefined" && a !== null

Until recently, RapydScript used == for equality and as a result the need for such check was somewhat rare. With === equality that the compiler now uses, such operator would be useful. Already I find myself checking against both null and undefined more and more often, this annoyance would further aggravate those coming from Python background and unfamiliar with JS quirks. Here are a couple alternatives I was thinking about for handling such an operator:

if exists(a):
    ...

Pros: concise, readable and doesn't introduce funky operators that may be unfamiliar with Python devs perl already has the same function, used for the same purpose (but typically for testing hash keys) Cons: Python devs may not be aware of the need for exists() test

if a is None:
    ...

Pros: already widely used in Python code, optimizer could then take care of this compilation for us is operator is currently useless, as it's identical to == Cons: there may be legitimate reasons for checking against null and not undefined, which this would break in Python, is is more strict than ==, this check would actually be less strict than == (however, JS === is already more like is in Python rather than == for object types)

atsepkov avatar Mar 29 '15 22:03 atsepkov

This is implemented in rapydscript-ng

kovidgoyal avatar Apr 15 '16 16:04 kovidgoyal

https://github.com/kovidgoyal/rapydscript-ng/commit/9c508b25d5a106e1ae4b51c9eb471fdb9143ebec

kovidgoyal avatar Apr 15 '16 16:04 kovidgoyal

FYI, still on the fence about this one. In your version, you changed the syntax for ternary conditional, so the choice of ? makes sense. In my version, like in CoffeeScript, the whitespace before that ? would completely change its meaning, which I don't like.

atsepkov avatar Oct 03 '16 07:10 atsepkov

Yeah, I can see how it is a problem for you. I got rid of the JS variant of the ternary conditional since there was no need for it, and it frees up the ? operator for this purpose. Given the prevalanece of undefined and null throughout the JS ecosystem, I for one find an existential operator very useful.

kovidgoyal avatar Oct 03 '16 07:10 kovidgoyal

Agree, I've gotten quite accustomed to using it in Swift.

I think you replaced the ternary with Pythonic conditional (http://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator) rather than getting rid of it completely, is that not the case? I find myself using the ternary quite often to make the assignment more clear:

a = condition ? then : else

vs

if condition:
    a = then
else:
    a = else

What I don't like about Python's version of ternary, however, is that it flips then and condition.

atsepkov avatar Oct 03 '16 07:10 atsepkov

Yes, I replaced it with python's ternary conditional operator. Since I write a lot of python code, python's ternary conditional has become quite natural for me :) Although, I agree, that it seemed awkward when I started using it. And I too use the ternary conditional a lot, in all languages I write.

Also, given that most of RapydScript's syntax is pythonic, it made sense to use the python form.

kovidgoyal avatar Oct 03 '16 07:10 kovidgoyal

As this discussion is covering the Python ternary operator, I'd like to note that it would be really nice the Python syntax was supported. I was surprised it is still not supported.

To have the same effect without it, and still have valid Python code, one needs to create a function for the branch logic, and because it is an expression that could appear anywhere (print(1 if 1 else 2)), that function needs to be injected into the scope of the expression, before the expression.

Glad to see that rapydscript-ng supports this; I'll need to use that until if-exp support is added to the original project.

jayvdb avatar May 29 '21 05:05 jayvdb

@jayvdb ternary operator was supported from day 1, it's just the JS syntax, not the Python one. As mentioned in the README and other discussions, the goal of the project was never 100% Python compatibility. The goal was to balance clarity and consistency of Python with JavaScript (and yes, there are cases when JS syntax is actually more consistent/clear than Python). To me, the ternary syntax of JS seemed cleaner than that of Python because it doesn't flip the conditional and if block. The ng version of the project attempts to stay more pure to the Python syntax (including objects/arrays) at the expense of some performance. You're more than welcome to use that, if it's more suitable to your needs.

atsepkov avatar May 29 '21 05:05 atsepkov