[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Self-initializing tables
- From: Philipp Janda <siffiejoe@...>
- Date: Tue, 23 Feb 2016 06:45:24 +0100
Am 23.02.2016 um 06:24 schröbte Dirk Laurie:
A module is attached that contains the following comments:
-- Self-initializing table. The point is that `tbl[key]` is never nil.
-- tbl = selfinit(tbl) -- makes an existing table self-initializing
-- and returns it
-- tblx = tbl.x -- tbl.x is an empty table with the same
-- metatable as x
-- tbl.a.b.c.d = 'item' -- tbl.a, tbl.a.b, tblc.a.b.c are created too,
-- even if `item` is nil.
-- tbl = selfinit() -- creates a new self-initializing table
-- WARNING: The implications are strongly counterintuitive.
-- 1. You will need `rawget` to find out whether a table entry exists.
-- 2. If `ipairs` is found by the module loader to treat an empty self-
-- initializing table as non-empty, it is monkey-patched to use `rawget`.
Use it at your own peril.
Something like this may well have been done before. Please supply
pointers; I'll gladly concede priority.