lua-users home
lua-l archive

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


On Jan 15, 2010, at 4:02 PM, Rob Kendrick wrote:

> Hi,
> 
> I find myself writing code that looks a lot like this quite often:
> 
> 	local foo, err = make_foo()
> 	if not foo then return nil, err end
> 
> 	local bar, err = make_err()
> 	if not bar then return nil, err end
 
I can't offer an explanation (except that my understanding is that the "local" keyword always brings its names into scope, shadowing all previous uses of that name), but this behavior should be avoidable with code like

	local foo, bar, err;

	foo, err = make_foo()
	if not foo then return nil, err end

	bar, err = make_err()
	if not bar then return nil, err end

Another oddity of this particular processing gimmick is the ability to write lines like

local _, _, _, _, test = someMultiReturnFunction()

This actually creates four local variables named _, but only the last one will be accessible.