metatables, __len

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Operarus
Prole
Posts: 7
Joined: Thu May 08, 2014 2:20 pm

metatables, __len

Post by Operarus »

When i write it, im get 0, but i want to get 100, please help;

Code: Select all

q = {};
setmetatable(q, {
  __len = function(self)
    return 100;
  end
});
error(#q);
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: metatables, __len

Post by zorg »

worksforme, both with "error(#q)" and "return #q". (on the online lua demo page)
maybe someone else can figure out your issue, it could be some version difference thing, like __len not existing before lua 5.2 or something.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: metatables, __len

Post by kikito »

I confirm that __len works in some versions of Lua and not in others. In vanilla it works in Lua >= 5.2.

I have just tried it in LuaJIT 2.0.3 and it does not work.

A good way to know if it is supported is by checking the expression type(_G.rawlen). If it's nil, then __len is not supported. If it's 'function', then __len is probably supported.
When I write def I mean function.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: metatables, __len

Post by zorg »

Looks like it, and some other 5.2 stuff depends on whether luaJIT was compiled with the -DLUAJIT_ENABLE_LUA52COMPAT flag or not; probably not the case with löve then.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
slime
Solid Snayke
Posts: 3161
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: metatables, __len

Post by slime »

Correct, the precompiled LuaJIT bundled with love in OS X and Windows isn't compiled with that flag, since it would prevent games whose code relies on some Lua 5.1-specific features from working properly.

(The flag only exists in the first place because the features it enables can break compatibility with code written for Lua 5.1).
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests