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

  • SEARCH
  • Home
  • 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 5965811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:41:30+00:00 2026-05-22T19:41:30+00:00

Lets say I have some object that I’ve defined elsewhere. Maybe it represents a

  • 0

Lets say I have some “object” that I’ve defined elsewhere. Maybe it represents a set of items, but is more complex than a simple table. Whatever it may be, it would be logical to iterate over it.

As such, it has a iterator method defined. So I can write this:

local myObject = AbstractObject:new()

for obj in myObject:iterator() do
    obj:foo()
end

What I’m wondering is if there is some metamethod trickery that I can do, which will allow me to write this:

local myObject = AbstractObject:new()

for obj in myObject do
    obj:foo()
end

So is there?

  • 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-05-22T19:41:31+00:00Added an answer on May 22, 2026 at 7:41 pm

    One slight change to your example would make the semantics a lot less painful:

    local myObject = AbstractObject:new()
    
    for obj in myObject() do
        obj:foo()
    end
    

    That way, you can use a metatable to define the __call metamethod to return myObject:interator(), with code that looks something like this in AbstractObject:new():

    setmetatable(newobject, {__call = function() return newobject:iterator() end})
    

    Without the iterator construction, you’ll be effectively reusing a single iterator for multiple iterations, which means you’ll need to keep the iterator state in the object/creation closure, and reset it after it finishes so the next call will restart the iteration again. If you really want to do this, the best solution would really be to write something for the specific iteration implementation, but this would perform the generic iteration:

    local iterator
    
    --table.pack is planned for 5.2
    local pack = table.pack or function(...)
      local t = {...}
      t.n = select('#',...)
      return t
    end
    
    --in 5.1 unpack isn't in table
    local unpack = table.unpack or unpack
    
    function metamethods.__call(...)
      if not iterator then
        iterator = newobject:iterator()
      end
    
      local returns = pack(iterator(...))
    
      if returns[1] == nil then
        --iteration is finished: next call will restart iteration
        iterator = nil
      end
      return unpack(returns, 1, returns.n)
    end
    

    Again: This should really be adjusted to fit your use case.

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

Sidebar

Related Questions

Lets say I have some domain objects that will need to be serialized/packed using
So lets say I have an object that it stored in a Database: com.test.dummy.Cat
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say I have a view, myView, a view controller, myViewController, and some sort
I have a name in a database, lets say its DFectuoso and some legacy
I have a fields_for tag, where I specify the prefix (lets say for some
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
Let's say I have some pointers called: char * pChar; int * pInt; I
Let's say I have some code like this if(isset($_GET['foo'])) //do something if(isset($_GET['bar'])) //do something

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.