Re: How to document an "unusual" API?
Posted: Tue Apr 05, 2016 12:06 pm
Okay I made my own versions using Lua "classes" (technically prototypes)
Passes all of the tests except checking for type.
Code: Select all
local Chain = {}
local Chain_mt = { __index = Chain }
local Invoker = {}
local Invoker_mt = { __index = Invoker }
function Chain:new( ... )
return setmetatable( {
...
}, Chain_mt )
end
function Chain_mt.__call( self, ... )
if not ( ... ) then
return self:run( select( 2, ... ) )
end
return self:append( ... )
end
function Chain:append( ... )
local offset = #self
for index = 1, select( '#', ... ) do
self[ offset + index ] = select( index, ... )
end
return self
end
function Chain:run( ... )
return Invoker:new( self, 1 )( ... )
end
function Invoker:new( chain, index )
return setmetatable({
chain = chain,
index = index
}, Invoker_mt )
end
function Invoker_mt.__call( self, ... )
local chain, index = self.chain, self.index
local link = chain[ index ]
if not link then
return
end
local go = Invoker:new( chain, index + 1 )
local returned = link( go, ... )
if returned then
returned( function ( _, ... )
go( ... )
end )
end
end
--]]
local function newChain( ... )
return Chain:new( ... )
end
return newChain
Code: Select all
T:assert(type(getmetatable(c1).__call) == 'function' ...