3 ms·
Thank you for the link, but I'm not sure this solves the problem. I mean, what if Abbie makes complex numbers, so she provides a metamethod to work with the bu
by xscott 6y ago
Thank you for the link, but I'm not sure this solves the problem. I mean, what if Abbie makes complex numbers, so she provides a metamethod to work with the builtin number type. Then Bobby wants to build on Abbie's work and add matrices. Lets say Abbie's complex type is on the left, but her metamethod doesn't know anything about matrices... Bobby is willing to support complex numbers multiplied with matrices, but the matrix metamethod never gets called:
z = complex(1, 2)
A = ident(3, 3)
-- Won't this next line call the wrong metamethod?
X = z*A
For a dynamicly typed language, I think you'd want something like multimethods to do this cleanly. (Or something like Python's dirtier approach)
- scythe 6y agoIf you want to allow unknown types to do your multiplying, you can write __mul like so: [...] elseif getmetatable(right).__mul and getmetatable(right) ~= complex then return getmetatable(right).__mul(left, right) [...] Of course this requires a little forward thinking, but it doesn't require you to know what other metatables (classes) you'll be compatible with. It's not as clean as multiple dispatch, though.