Joao Cruz Morais wrote:
I'm sorry if I'm saying something that was already proposed, but why
not use the Java approach to labeled breaks or something like this
(dumb) example:

while(true) as outer_cycle {
  $i = 0;
  while(true)
    if($i++ == 10) break outer_cycle;
}

well, this is only handling one of the three GOTO use cases
that we seem to agree on as being 'ok'

* escaping deeply nested loops or if statements

  this one it would handle, just as "break _number_;"
  does right now, so it would be a viable option even
  in parallel to GOTO or as a fallback position if we
  can't reach agreement on GOTO at all

* jumping to a central error handling/cleanup point

  this it can't handle unless you use it in combination
  with an extra $error flag
  while this is a viable approach i prefere

    for (...) {
      for (...) {
        for (...) {
          ...
          if (something_failed()) goto error_handling;
          ...
        }
      }
    }
    return true;

    error_handling:
    ...
    return false;

  to

    $error_flag = false;
    for (...) as outer_loop {
      for (...) {
        for (...) {
          ...
          if (something_failed()) {
             $error_flag = true;
             break outer_loop;
          }
          ...
        }
      }
    }

    if ($error_flag) {
      ...
      return false;
    }

    return true;

  as now there are all those extra $error_flag lines
  and to see where the error condition is actually processed
  i first have to search up from the 'break' to the outer_loop
  definition, then down to the closing { of that loops body
  and than further on to the place where $error_flag is
  actually checked (if at all)

  with "goto error_handling;" on the other hand i just have
  to search for "error_handling:" and i'm at the error handling
  code in one step (and the compiler can even guarantee that
  the error_handling: label exists whereas a flag might actually
  end up being ignored and an exception might get all the way
  up the call stack without being called)

* state machine implementations

  states get labels, state transitions goto labels -> pretty clean

  right now you have to implement stuff like this as switch()
  statements with an extra state variable being managed

  this has the advantage of being able to tell which state you are
  currently in and to have state transition logging in front of
  the switch, but i experience it as less readable and it doesn't
  perform as well as a GOTO based solution would as GOTOs can be
  resolved at compile time while switch requires run time comparisons


--
Hartmut Holzgraefe, Senior Support Engineer                            .
MySQL AB, www.mysql.com

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to