Log In  

I wrote a blog post about making a generic method dispatch helper for 'foreach'.
It's more of a novelty than something really worth adding to code. It's better to just use 'for x in all(XS)' instead.

Here's the mini summary:

function callm(method, ...)
 local params={...}
 return function(o)
  if type(o)=="table" then
   local m=o[method]
   if type(m)=="function" then
    m(o,unpack(params))
   end
  end
 end
end

function _draw()
 cls()
 foreach(drawable, callm("draw", 11))
end

Check it out: https://kallanreed.com/2021/10/17/pico-8-generic-dispatch-with-parameters/

P#98795 2021-10-17 17:56 ( Edited 2021-10-18 02:09)


[Please log in to post a comment]