On 21/12/2019 23:59:27, Chris Angelico wrote:
On Sun, Dec 22, 2019 at 10:46 AM Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:
On 22/12/19 9:04 am, Soni L. wrote:

switch (op) {
    [...]
    case OP_TAILCALL: {
      adjust_regs();
      some_other_stuff();
      /* fallthrough */
    }
    case OP_CALL: {
      make_the_call_happen();
      break;
    }
}
Relying on fall-through in a switch is a micro-optimisation that
may help make things go faster in C, but not so much in Python,
so trying to find a literal translation is probably wrongheaded.

I would be inclined to separate it into two independent cases:

      if op == OP_TAILCALL:
          adjust_regs()
          some_other_stuff()
      elif op == OP_CALL:
          adjust_regs()
          some_other_stuff()
          make_the_call_happen()

If the parts being duplicated are more than just one or two
calls as above, I would factor them out into a separate function.
(You have the translation backwards here; a tail call should do three
things while a non-tail call should do one.)

Or, if you want the two cases to continue to share code (which might be
desirable if the shared code was (a lot) longer than one line):

    if op == OP_TAILCALL or op == OP_CALL:
        if op == OP_TAILCALL:
            adjust_regs()
            some_other_stuff()
        make_the_call_happen()


While there is a repeated test which might offend purists, IMO the intent is clearer than the original "switch" version
where it is easy to overlook that execution is supposed to fall through.
IMO it is also clearer than "or if", though we wouldn't know for sure until/unless "or if"
was adopted in Python and we had had time to get used to it.

Rob Cliffe
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/GNZM326BHAENXVZYKFJ2S3DEMOLYWFCD/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to