Metatable query

Ah, bummer. You should play with them some time, they are quite interesting little tools; very powerful as well! They do many fun things, such as allowing you to perform arithmetic on two vectors (a Userdata type).
I have searched, but no luck on the specific thing I plan to do with the metatable, unfortunately (there were plenty of other cool things I learned in my search, though!). I’m hoping someone else will answer soon (or I at least hope there’s someone as confused and intrigued by my results as me who is working to figure it out, as I am).

@Monkeyman32123 - I suggest adding “Metatable query” to the end of the subject thread temporarily, to attract the attention of metatable experts.

Someone like @SkyTheCoder may be able to help you.

wrt the … parameter, another useful technique is to pass a table of items through to a function, this is very useful where you have many parameters and don’t awlays want to specify them all, see here

http://coolcodea.wordpress.com/2013/05/26/65-3d-managing-parameter-lists/

I know I can use tables, there are just a few instances where I’d rather use this new method (hence why I jestingly said I hate braces :P). That said, thank you! And I changed the subject name

With regard to metatables, I just threw that into lua on my laptop and got essentially the same results. You can define a new operator on numbers, such as length, but not redefine an old one. Searching for information on metatables and numbers shows that they’re a bit special, but I haven’t been able to pin down exactly how. It looks as though numbers are so integral to the smooth running of lua that you aren’t allowed to do silly stuff like redefining addition (as an example of what might go wrong, if you redefine addition then how are you going to cope with a for loop?).

So I recommend that you define a wrapper around the numbers for “quantum numbers” and work with that.

@Monkeyman32123 I don’t know much about metatables, but a little more since you asked the question. From what I’ve seen so far, you can’t override the +*/- operators when using numbers. At least I haven’t found anything that shows how. Below is an example I created that does it in a round about way. I changed the add, sub, mul, and div operators to do something different than what they’re supposed to do. Don’t know if this will help you any, but at least I learned something about metatables doing this.


--# Main

function setup()
    mt={}
    mt.new = function(s)
        setmetatable(s,mt)
        return s 
    end
    mt.__add = function(a,b) -- add changed to subtract
        return a[1]-b[1]
    end
    mt.__sub = function(a,b) -- subtract changed to multiply
        return a[1]*b[1]
    end
    mt.__mul = function(a,b) -- multiply changed to divide
        return a[1]/b[1]
    end
    mt.__div = function(a,b) -- divide changed to add
        return a[1]+b[1]
    end
    print(mt.new{56} + mt.new{79})
    print(mt.new{56} - mt.new{79})
    print(mt.new{56} * mt.new{79})
    print(mt.new{56} / mt.new{79})
end

Ah, thank you both muchly :slight_smile:
@dave1707 that isn’t exactly what I wanted to do, but still a very interesting example of what one can do with meta tables!

For an example of how to use metatables to augment or modify arithmetic, take a look at my Quaternion library https://gist.github.com/loopspace/5611542#file-quaternion-lua