Luup, pseudo object oriented, call_delay

Hi,
sorry about the weird topic. i have to explain that:

I found a solution to program lua in pseudo object oriented way. A class could look like this:

Class = 
{
	new = function(someText)
		local self = {}
                
                -- member variables
                self.text = someText
		
		self.doSomething = function()
			luup.log("Text: "..self.text, 01)
		end
		return self
	end	
}

In the main or init function that class could be used like this:

function main()
   require("Class")

  local testClass = Class.new("Hello World")
  testClass.doSomething()
end

This works like it should. But i want a class to do something asynchronously in a loop. So i changed to code to the following:

Class = 
{
	new = function()
		local self = {}
		
		self.run = function()
			luup.call_delay("self.print", 0.1, "HELLO", 1)
		end
		
		self.print = function(data)
			luup.log("DATA: "..data, 02)
		end
		
		return self
	end	
}

Now the function “self.print” can?t be found. If i make this function static

Class = 
{
	new = function()
		local self = {}
		
		self.run = function()
			luup.call_delay("print", 0.1, "HELLO", 1)
		end
		
	        print = function(data)
			luup.log("DATA: "..data, 02)
		end
		
		return self
	end	
}

it works, but because the function is static now i can?t use member variables of the class.

Does anyone know a solution for this?

Thank you!

What you’ve done is about as good as you can do. Functions registered with luup.call_delay() have to be in the global symbol table; it’s an unfortunate limitation and not one that you can work around.

Your OO technique is useful in other languages too. They are called closures and they are how you do OO in JavaScript and Perl and Python.

Thanks futzle for that information.

i?ve feard that.