Re: Can you return from inside a switch?

new topic     » goto parent     » topic index » view thread      » older message » newer message
ghaberek said...

To answer this explicitly

In the same spirit of clarity it is probably also worth mentioning this:

while some_condition do 
  switch x do 
    case a then 
      break -- [1] 
    case b then 
      exit -- [2] 
  end switch 
  ... 
end while 

Which is perfectly fine on both Euphoria and desktop/Phix, with [1] resuming after the end switch and [2] resuming after the end while.
However, under "with js" (on Phix), since JavaScript uses the "break" keyword for both and has no goto statement, [2] becomes illegal.
It is a limitation of JavaScript, not Phix nor Euphoria, the error message should be pretty clear, just something you should know.
One fix would be to replace the exit with setting a flag and adding a test/exit statement just after the end switch.
A return statement remains perfectly fine in all cases.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu