Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add existential 'operator' #42

Open
atsepkov opened this issue Mar 29, 2015 · 8 comments
Open

add existential 'operator' #42

atsepkov opened this issue Mar 29, 2015 · 8 comments

Comments

@atsepkov
Copy link
Owner

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)

@kovidgoyal
Copy link
Contributor

This is implemented in rapydscript-ng

@kovidgoyal
Copy link
Contributor

@atsepkov
Copy link
Owner Author

atsepkov commented Oct 3, 2016

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.

@kovidgoyal
Copy link
Contributor

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.

@atsepkov
Copy link
Owner Author

atsepkov commented Oct 3, 2016

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.

@kovidgoyal
Copy link
Contributor

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.

@jayvdb
Copy link

jayvdb commented May 29, 2021

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.

@atsepkov
Copy link
Owner Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants