lua-users home
lua-l archive

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


On Dec 17, 2015 6:26 PM, "Thijs Schreijer" <thijs@thijsschreijer.nl> wrote:
>
> I ran into the following;
>
> local v = {type = "timestamp"}                          -- added to run it
> if v.type == "number" or v .type == "timestamp" then    -- actual code
> end
>
>
>
> Notice above, the second `v.type` has a typo with an extra space; `v .type`. I'd expected an error, but it runs fine.
>
> Why?
>
> Thijs
>
>
Because whitespace between tokens is usually ignored in Lua and most other languages.

The exception is when it's ambiguous:
x = f
(a or b):c()
That _expression_ could be interpreted as two statements or one (remove the line break and see). In that case Lua is kind enough to make an exception to the "ignore whitespace" rule and raise an error (which you can resolve by adding a semicolon or rearranging the statements to remove the ambiguity), instead of just strictly enforcing precedence rules and doing what you may not expect.

Fun fact: comments are treated like whitespace too:
x = --[[ my lucky number: ]] 3