lua-users home
lua-l archive

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


On Aug 23, 2010, at 2:11 PM, Cuero Bugot wrote:

> The problem happens (for example) when you use multiple level tables to store configurations. For example, you store something like:
> 	config.network.server.url = "xxx"
> If the parameter is optional, or if you do not want bad surprise, when you want to use it you need to write:
> 	if confg.network and config.network.server and config.network.server.url then
> 		dosomething(config.network.server.url)
> 	End
> 
> This is quite verbose, and it could be nice if it returned nil if one of the level of "config.network.server.url" is nil...

Perhaps not as succinct, nor transparent as desired, but still:

local config = {}

if ( ( config[ 'network' ] or {} ) [ 'server' ] or {} ) [ 'url' ] == 'example.com' then

Alternatively:

debug.setmetatable( nil, { __index = function() return {} end } )

local config = {}

if config.network.server.url then