[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Final optional expression
- From: Sean Conner <sean@...>
- Date: Sun, 20 Jun 2021 17:02:34 -0400
It was thus said that the Great Andrew Yan once stated:
> In Lua 5.3, this code is unparseable:
> ```
> function huh() 1 end
> ```
> Would it be possible in future versions of Lua to instead make it return
> `1` when called, i.e. to permit a final optional expression? AFAIK this
> wouldn't break anything since the above statement can't be parsed anyways.
>
> The motivation for this is lua configuration files.
> Example:
> `conf.lua`
> ```
> -- statements...
> {
> option1=1;
> option2=2;
> }
> ```
You can get the same effect by removing the table constructor:
===conf.lua===
option1 = 1
option2 = 2
===
To read in the file:
local conf = {}
local f,err = loadfile("conf.lua",'t',conf)
if not f then error(err) end
f()
-- now you have conf.option1 and conf.option2
I have used this method multiple times with no issues. You can check out:
https://github.com/spc476/port70/blob/master/port70.lua#L46 (code)
https://github.com/spc476/port70/blob/master/sample-conf.lua (sample config file)
for an example.
-spc