
On 2019-12-21 5:22 p.m., Chris Angelico wrote:
On Sun, Dec 22, 2019 at 7:06 AM Soni L. <fakedme+py@gmail.com> wrote:
On 2019-12-21 4:52 p.m., Chris Angelico wrote:
I'm not sure I understand what switch construct would translate into this style. Can you show an example of code in some other language, how you'd translate that same logic into today's Python, and how you'd translate it into this "and if" / "or if" style?
Lua's VM had a case that went like this:
switch (op) { [...] case OP_TAILCALL: { adjust_regs(); some_other_stuff(); /* fallthrough */ } case OP_CALL: { make_the_call_happen(); break; } }
This is for what's known as "tail-call optimization". Anyway, if one were to translate this with "or if", it would look like:
if op == OP_TAILCALL: adjust_regs() some_other_stuff() or if op == OP_CALL: make_the_call_happen()
Importantly, note how there's no need for a "break" statement, and the fallthrough is explicitly stated in the use of an "or if" statement.
Ah, okay. I get you. So you'd translate a switch block into a series of "elif" statements, but with an "or if" being a fallthrough case. So in effect, an if block would look like:
if first_condition: first() elif second_condition: second() # fallthrough or if third_condition: third() elif fourth_condition: fourth() else: default_action()
After calling second(), this would skip the third condition check, and go straight into third(). That's a concept that currently is quite hard to express cleanly in Python. (In contrast, the "and if" logic doesn't seem as useful; just nest it inside.)
I'm not a fan of the "or if" spelling, but that's a matter of
I decided on "or if" because I didn't wanna introduce new keywords, and it seems to fit conceptually. Ideally I'd go with "orif" but, well, then you'd introduce new keywords.
bikeshedding. The main question is: How common is this code, in situations where there is no better way to represent the entire block (ie you can't just rewrite it as a dict lookup)? I'd be curious to hear from anyone who's writing compilers in Python (including but not restricted to PyPy), and how they go about writing key constructs like this.
ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/ZGDZ3A... Code of Conduct: http://python.org/psf/codeofconduct/