[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Safe navigation operator
- From: Cuero Bugot <cbugot@...>
- Date: Mon, 23 Aug 2010 05:11:16 -0700
I came across a case where I found no short way to write it in Lua. This case could be solved with a "Safe navigation operator" (at least it is the way it is called in Groovy).
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
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...
(See Safe navigation operator in groovy)
So the next step was "Well Lua is wonderful, it'll be easy to implement that 'specific' behavior with metamethods!". But after a little though and tries out, I realized that this use case was not coverable with __index / __newindex, because those metamethods lacks a 'context'.
Does somebody already encountered/solved that use case ?