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?
P#87370 2021-02-08 02:01
:: freds72
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)
|
P#87384 2021-02-08 11:58 ( Edited 2021-02-08 12:02)
:: ChristopherD
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.
P#87404 2021-02-09 00:58
[Please log in to post a comment]



