[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Gotchas in math metamethods etc
- From: HyperHacker <hyperhacker@...>
- Date: Sat, 12 Mar 2011 22:15:20 -0700
Something I stubbed my toe on the other day:
> obj={val=2}
> meta={}
> function meta:__mul(num) return self.val * num end
> setmetatable(obj, meta)
> =obj*4
8
> =4*obj
stdin:1: attempt to index local 'self' (a number value)
stack traceback:
stdin:1: in function <stdin:1>
stdin:1: in main chunk
[C]: ?
While this makes perfect sense, it seems like something that's likely
to give a hard time to newbies who don't realize why the second
statement fails ('self' is actually 4). I was just thinking this
should be noted in the manual to avoid that potential confusion. True,
the pseudocode example for __add shows exactly what's going on, but
it's one of those subtle things you're likely to not really think
about at the time.
--
Sent from my toaster.