-- A simple index fallback implementation for tables.-- Useful for template argument aliasing.-- Hmm, what about __newindex?localfunctionmakeFallback(args,arg_aliases)localoldArgsMeta=getmetatable(args)or{}localnewArgsMeta={}-- Forget about thread-safety.-- States kept to avoid strange loops.localreferencedKeys={}-- dataType:Set/hashtable-impllocalattemptDepth=0-- useful for "last nil" case-- Start the new metatable as a copy of the old metatable.fork,vinipairs(oldArgsMeta)donewArgsMeta[k]=vend-- Change the __index metamethod to our implementation.-- See https://www.lua.org/pil/13.4.1.html.newArgsMeta.__index=function(t,k)-- My friend, why are you here again?ifreferencedKeys[k]then-- You have to be drunk. Go home.returnnilendreferencedKeys[k]=true-- Try the old metamethod first.-- Thanks to closures, this whole oldArgsMeta object will stay.ifoldArgsMeta.__index~=nilthenlocalval=oldArgsMeta.__index(t,k)ifval~=nilthenreferencedKeys={}returnvalendendattemptDepth=attemptDepth+1-- Now try use the aliases given.for_,vinipairs(arg_aliases[k]or{})do-- If a working value is found, use it.-- Note: mw-argument-specific empty str chk.ift[v]~=nilandt[v]~=''thenreferencedKeys={}returnt[v]endendattemptDepth=attemptDepth-1ifattemptDepth==0thenreferencedKeys={}endreturnnilendsetmetatable(args,newArgsMeta)returnargs-- just in case someone wants a return value.endreturnmakeFallback--[[P.S. Don't blame me for writing these obj-states. Sure, we can use anaccumulator and do some "index with acc" ourselves, but that sounds like toomuch work done just to avoid states.Here is that custom index function in case anyone is curious about it: function index_w_acc(t, k, acc) local v = rawget(t, k) if v ~= nil return v else return getmetatable(t).__index(k, acc) end endMy guess is that it will certainly be slower than the native t[k] one. Perhapsnot getting the metatable every time will help a bit.]]--