This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CometWolf's profile picture

[1.58] __len metamethod does not work

Started by CometWolf, 21 March 2014 - 11:58 PM
CometWolf #1
Posted 22 March 2014 - 12:58 AM
After a few hours of debugging my own program, i've come to the conclusion that the __len metamethod defined in the Lua wiki does not work.
Running the following code

test = setmetatable({},{__len = function(t) return "Invoked __len function on "..tostring(t) end})
return #test
In the online lua demo returns as expected
"Invoked __len function on table blah blah"

However, executing the same piece of code on computercraft returns
0
Meaning the __len function isn't invoked at all.
Edited on 22 March 2014 - 07:21 AM
sci4me #2
Posted 22 March 2014 - 03:43 AM
Just create a function called length…
I think this is only for lua 5.2… and CC is 5.1.
Easy to solve though…
theoriginalbit #3
Posted 22 March 2014 - 03:48 AM
Just create a function called length…
not the point that was trying to be made, __len is to add the support for the # character
sci4me #4
Posted 22 March 2014 - 08:12 AM
Just create a function called length…
not the point that was trying to be made, __len is to add the support for the # character

I know but (someone really needs to confirm this, im 99.627 percent sure..) in lua 5.1 you cant. Only 5.2.
CometWolf #5
Posted 22 March 2014 - 08:25 AM
I thought as much myself originally, but it's listed in the 5.1 manual
http://www.lua.org/manual/5.1/manual.html#2.8

Edit: looking closer at it, it would seem it's only invoked on variables that are not tables or strings. How stupid…
Edited on 22 March 2014 - 07:27 AM
Engineer #6
Posted 22 March 2014 - 03:28 PM
I thought as much myself originally, but it's listed in the 5.1 manual
http://www.lua.org/m...manual.html#2.8

Edit: looking closer at it, it would seem it's only invoked on variables that are not tables or strings. How stupid…
in Lua 5.2 it is invoked on those iirc. It was a bummer for myself when a friend of mine discovered thi either
sci4me #7
Posted 22 March 2014 - 05:05 PM
Yes, it is very unfortunate. The ability to override __len would be really nice…