I’m not sure if this is a lua question or a codea question - I’m new to both.
Is it possible to define a function that can, say, take either a vec2 object or two numbers?
In perl, I’d look at the first argument and test to see if it were an instance of the vect2 class (side question: is there a test for this?). If not, I’d read in the next argument as well. This works because Perl functions don’t specify their argument types, but it looks as if lua does.
(If this is answered in some general lua documentation, please take pity on me and point me to the right place! Thanks)
You can ask for the type of a value but all objects are just tables. There’s no standard way of telling if a table is an instance of a class defined with the class() function.
You could use Lua’s varargs. If the function has two args they are x & y, if one arg it’s a vec2.
But style-wise I think that two functions with different names is better. And will be more efficient.
Yes. if you ask for (a, b, c), and they pass (a) - b and c will be nil. You can check type() of the argument to see if a was a number or a hash, and respond appropriately.
You can also do a named argument thing with the hash as well - I like that for stuff that could have potentially many arguments, but usually has reasonable defaults - you pass only what’s different.
I’ve had issues as well - a big chunk of my brain is hardwired for Perl, and Lua is just different enough that I keep making assumptions that seem reasonable - but aren’t. My brain said “Ah - tables = Perl Hashes”, and that’s just wrong enough to screw me up.