Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:59:41+00:00 2026-06-12T11:59:41+00:00

A beginner’s question about Lua and metatables , with a example as simple as

  • 0

A beginner’s question about Lua and metatables, with a example as simple as an Hello‑World, involving the len event, which unfortunately does not returns the expected result (I’m using Lua 5.1 installed from Ubuntu’s official repository).

The case

Here is the example:

Test_Type = {};

function Test_Type.__len (o)
   return 1;
end;

function new_test ()
   local result = {};
   setmetatable(result, Test_Type);
   return result;
end;

do
   local a_test = new_test();
   print (#a_test);
   print(getmetatable(a_test).__len(a_test));
end;

And the result I get:

0
1

I was expecting the first print statement to display 1, but it displays 0, to my big surprise.

What did I missed?

According to Lua Reference Manual — Metatables and Metamethods, the # is equivalent to this:

function len_event (op)
  if type(op) == "string" then
    return strlen(op)      -- primitive string length
  else
    local h = metatable(op).__len
    if h then
      return (h(op))       -- call handler with the operand
    elseif type(op) == "table" then
      return #op              -- primitive table length
    else  -- no handler available: error
      error(···)
    end
  end
end

So print (#a_test); and print(getmetatable(a_test).__len(a_test)); should result into the same, isn’t it?

By the way, why is the above excerpt from the Reference Manual refers to metatable(op) while it should be getmetatable(op)? At least I’ve tried print(metatable(a_test).__len(a_test));, and it ends into an error.

Answer

As Nneonneo noticed, this is an issue with the Lua version in use. Lua 5.2 seems to be required for the above to work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:59:43+00:00Added an answer on June 12, 2026 at 11:59 am

    From http://lua-users.org/wiki/LuaFaq:

    Why doesn’t the __gc and __len metamethods work on tables?

    __len on tables is scheduled to be supported in 5.2. See LuaFiveTwo.

    Since you’re using 5.1, __len on tables does not work. Indeed, running your code on Lua 5.2 produces

    1
    1
    

    as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner help needed :) I am doign an example form a php book which
Beginner here, I have a simple question. In Android what would be the best
Beginner question. I'm making a simple Gtk notepad in C# and MonoDevelop and everything
Beginner to assembly programming for x86. I have a simple asm file which I
VS beginner question. How to change parent control of a control? Example. I have
I'm a beginner in jQuery area and I have simple question like this :
Beginner page building question: I've got a simple /div container defined via a CSS
Beginner Question. Below is an example given on the Cradle CouchDB documentation: https://github.com/cloudhead/cradle What
Beginner Git question: In the Mercurial world, hg tags gives me a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.