[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A very basic thing I don't get
- From: Michal Kottman <k0mpjut0r@...>
- Date: Sat, 1 Oct 2011 19:55:25 +0200
On 1 October 2011 19:43, Stefan Reich
<stefan.reich.maker.of.eye@googlemail.com> wrote:
> On Sat, Oct 1, 2011 at 7:32 PM, Peter Cawley <lua@corsix.org> wrote:
>
>> The message to take away from this is that things are truncated to
>> exactly 1 result iff they are not the last thing in an expression
>> list.
>
> Why is that done? Strikes me as rather confusing. (The OP obviously
> fell for it too.)
The other way round is also confusing sometimes - some functions
return 'nil, error' in case they fail. This is a common idiom in
places where you do not want to throw an error (like io.open()).
Let's imagine a situation that you want to open several files, and
store them in array:
-- works in standard Lua
openFiles = {io.open('file1'), io.open('file2'), io.open('file3')}
for i=1,3 do
if openFiles[i] then -- either a file handle or nil
print(openFiles[i]:read('*a')) -- may fail here if also the
errors were stored in the array
end
end
If the array constructor worked as you think it should, in case a file
cannot be opened, a {..., nil, string, ...} would be stored in the
array, causing an error in your loop.
Also this would also fail if you used the result of io.open() as a
parameter of a function, i.e.: processFile(io.open('file'), other,
params). In case it failed, 'other' and 'params' would be shifted by
one.
I'm not saying that this is definitely the reason multiple values
currently work, but your way would IMHO cause much more confusion - it
is harder to debug when parameters suddenly shift positions and values
end up in different variables :). When the 'expansion' only happens at
the end, it is "safer", because it cannot overwrite any parameters.