3 ms·
I had heard that Lua was adding const support, which I was excited for, but I hadn't seen the syntax until now. I don't care for it. I'm guessing this is due to
by aquova 6y ago
I had heard that Lua was adding const support, which I was excited for, but I hadn't seen the syntax until now. I don't care for it. I'm guessing this is due to ambiguity as to whether variables declared as const would be local or global, but I would rather have something like
const x = 1
local const y = 2
over what they're adding.
- ufo 6y agoThe 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.