Re: Try/Catch
- Posted by dcuny Jan 09, 2015
- 5608 views
I should note that in Lua, pcall{} either returns false, or true plus additional parameters.
From that perspective, you could write:
{error_flag, result1, result2} = protected_function( foo() ) if error_flag then -- handle the exception end if
But I liked decoupling the assignment and flag because there's no need for the temporary variable.
Plus, protected_function() would have to figure out if a sequence returned foo was a single value, such as:
s = foo1() -- returns a sequence of characters {x, y} = foo2() -- returns a sequence containing x and y
So protected_function() would have to know what the left hand assignment looked like, so it could know whether to return a new sequence or append to the result. There's nothing in the declaration of the routine that indicates what to do:
{error_code, s} = protected_code( foo1() ) -- has to create a new sequence to hold error_code and result of foo1 {error_code, x, y} = protected_code( foo2() ) -- has to append error_code to the sequence return by foo2
I think passing the parameter list of variables to assign is more elegant.
Personally, I still think try/catch is the better route because it's more familiar.
And if you ultimately implement the C++ version using try/catch... it's even more of a no-brainer.
- David