Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
reteyez

require recursion

Question

Im trying to create a utility "class" wich returns "allObjects" either by function or directly.

Objects are files within a specified folder with a specified extension.

  local Handler = {object = {}}
  local workspace = {
    shell.getWorkingDirectory().."dir1",
    shell.getWorkingDirectory().."dir2"
  }

  for key, dir in pairs(workspace) do
    for element in fs.list(dir) do
      local s, e = string.find(element, "%.")
      local ext = string.sub(element, s + 1)
      local title = string.sub(element, 1, s - 1)

      if ext == "foo" or ext == "bar" then
        if Handler.object[ext] == nil then Handler.object[ext] = {} end
        result, Handler.object[ext][title] = shell.execute(dir.."/"..element)
      end
    end
  end


  return Handler

Works actually fine but the problem is that I need to require the handler in the object files themselfs since they need to know eachother.

This though leads to a problem since I also use the handler in the main class which when called gives the error:

"already loading: handler"

 

Example of an object ("dir1/obj1.foo"):

local handler = require("handler")

local obj1
obj1.title = handler.object.foo.obj2.title

return obj1

The main file needs the handler aswell:

local handler = require("handler")

--For example
for k, v in pairs(handler.object.foo)
  v.run()
end

which isn't a suprise. I also tried shell.execute instead of require which ended in an endless loop. I understand the problem but I can't think of an elegant way to solve it.

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
  • Solution

Relevant Lua 5.3 manual section in case you want to know more about require: http://www.lua.org/manual/5.3/manual.html#pdf-require

The error says that handler is currently being loaded when it calls itself. Here's a possible workaround:

  local Handler = {object = {}}
  local workspace = {
    shell.getWorkingDirectory().."dir1",
    shell.getWorkingDirectory().."dir2"
  }

  function Handler.init()
    local key, dir
    for key, dir in pairs(workspace) do
      local element
      for element in fs.list(dir) do
        local s, e = string.find(element, "%.")
        local ext = string.sub(element, s + 1)
        local title = string.sub(element, 1, s - 1)

        if ext == "foo" or ext == "bar" then
          if Handler.object[ext] == nil then Handler.object[ext] = {} end
          local result
          result, Handler.object[ext][title] = shell.execute(dir.."/"..element)
        end
      end
    end
  end

  return Handler
and then in your main code:

local handler = require("handler")
handler.init()

--For example
local k, v
for k, v in pairs(handler.object.foo)
  v.run()
end
Also, use local more. Otherwise, you can easily bite yourself in the arse like so:

function getkey(t, kval)
  for k, v in pairs(t) do
    if v == kval then return k end
  end
  return nil
end

for k, v in pairs(foo) do
  k2 = getkey(v, "derp")
  if k2 then print(k2, v[k2]) end
end
Link to post
Share on other sites
  • 0

Thanks for the answer. Yea that would be a way to do it. I did it totally different by now though :). I use dofile instead of shell.execute now and the object table is just a local table inside the Handler (which I access with Handler.functions) so it gets passed as upvalue to the file that I run with dofile. And thanks for the tip with the locals, I'll keep it in mind.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.