from Hacker News

Everything You Didn't Want to Know About Lua's Multi-Values

by benaiah on 5/4/20, 9:09 PM with 7 comments

  • by jmaa on 5/5/20, 5:01 PM

    I do not really agree with the article's (implied?) statement that multivalues are an ugly part of Lua. It's subtle yes, but like most of Lua, is incredibly consistent.

    For example, let's compare with Python: In Python, you can return multiple arguments as a tuple; it's not possible to have an optional return value without a lot of painful type matching and unpacking. In Lua you can:

      local result, errmsg  =  do_stuff()
      if not result then
        error("Error in do_stuff:"..errmsg)
      end
      print(result + 5)
    
    If you don't want the error message, just remove ", errmsg", and it will work as expected. This is incredibly flexible, which is exactly what I want in my dynamic languages. In Python, you would get an type error when attempting to add together a tuple and an integer.