4 ms·
The problem with putting the "const" first is that it is ambiguous when there is more than one variable. For example, suppose that you want to open a file and t
by ufo 6y ago
The problem with putting the "const" first is that it is ambiguous when there is more than one variable. For example, suppose that you want to open a file and test for errors:
local f <close>, err = io.open("foo.txt")
if not f then error(err) end
vs
local <close> f, err = io.open("foo.txt")
if not f then error(err) end
In the first version it is clearer that only the "f" variable is marked as a to-be-closed variable. The second version looks nicer when there is a single variable but is more confusing when there are multiple variables.
- Pxtl 6y agoEither way, why are the <> necessary? local f close = io.open("foo.txt") seems fine.
- ufo 6y agoThat wouldn't work because it is already valid Lua code with a different meaning. Lua parses it as a declaration of a local variable called f followed by an assignment to a variable named close: local f close = io.open("foo.txt") Despite the lack of semicolons, Lua is a free-form language and newlines are treated the same as any other whitespace character.