I have a number of functions which, before doing any real work, need to do the exact same pre-processing routine of the passed parameters. As such, they all start like this...
local p1, p2, error = preprocess(param1, param2) if (error) return error --early return |
Is there a way to use metatables to inject this kind of default behavior into functions, without having to copy/paste that boilerplate code each time?



not metatable but varargs can help:
function preprocess(fn,...) local args={...} — check args — — call real function return fn(...) end function do_a(a,b) return preprocess(function() return a/b end,a,b) end function do_b(a,b) return a/b end — wrapping style 1 do_a(12,35) — wrapping style 2 preprocess(do_b, 12,35) |



Thanks @freds72
Funny enough, I do something similar in another part of my code, but for some reason couldn't recognize the pattern being applicable in this case.
[Please log in to post a comment]