lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I recently found this, and thought it was worth bringing to people's
attention:

function test()
  print(1)
  return
  print(2)
end

test()

- ->
1
2

Surprised? I was, until I realised what was going on --- remember that
return and break must be the *last* statement of a chunk; the code is
being parsed like this:

function test()
  print(1)
  return (print(2))
end

The second print statement is emitting '2' as a side effect and
returning nil.

This, of course, does what I expect:

function test()
  print(1)
  do return end
  print(2)
end

...and if there's more than one statement after the return then the code
won't compile and it's obvious what's going on.

- --
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "There is nothing in the world so dangerous --- and I mean *nothing*
│ --- as a children's story that happens to be true." --- Master Li Kao,
│ _The Bridge of Birds_
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFK5MXif9E0noFvlzgRAgb/AJ9IMYSddQYHJE+K5MMCukMkw9uA1ACff6i+
ALEo+8k3XGhJDqeoZSYhkdo=
=Agah
-----END PGP SIGNATURE-----