[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Assign idiom
- From: "Alexander Gladysh" <agladysh@...>
- Date: Thu, 8 Mar 2007 20:47:36 +0300
>> local sp = self.spBackGround = CreateSprite()
>> sp:setSize( 10, 10 )
What about something like this? (All code right from my head, without
syntax check.)
-- Meaning a member function defined somewhere
self.InitBGSprite = function(self)
self.spBackGround = CreateSprite()
return self.spBackGround
end
local sp = self.InitBGSprite()
Or even this (looks like overkill though, I like previous one more):
-- Meaning a member function defined somewhere
self.InitVar = function(self, name, ctorfn, ...)
local v = ctorfn(...)
self[name] = v
return v
end
local sp = self.InitVar("spBackGround", CreateSprite)
-- If your CreateSprite() accepts some extra arguments, say, size,
-- you can do this instead:
local sp2 = self.InitVar("spBackGround", CreateSprite, 800, 600)
If you're going to use this idiom very often, you can use combined approach:
-- A global function constructor
MakeInitializer = function(name, ctorfn)
return function(self, ...)
local v = ctorfn(...)
self[name] = v
return v
end
end
-- Meaning a member function defined somewhere
self.InitBGSprite = MakeInitializer("spBackGround", CreateSprite)
local sp = self.InitBGSprite()
-- or --
local sp1 = self.InitBGSprite(800, 600)
HTH,
Alexander.