Re: MiddleClass & MindState: Object Orientation for LUA
Posted: Wed Apr 14, 2010 7:43 pm
problem resolved, thanks
No you don't.kikito wrote:ah, I hate those.
The : operator is actually one of the only things I don't like much about Lua. I wish it was reversed - so the dot included "self" by default and the colon didn't.
Code: Select all
love:graphics:print(self:class:name, self:x, self:y)
P*thon does that, but Lua's way has two distinctive advantages:kikito wrote:That, and variables being local by default.
That is not what I meant. Let me be a bit more precise: First, I only meant it to be used on method calls: love.graphics, self.class.name, self.x, self.y would work just the same as they do.Robin wrote:No you don't.
Consider this:Code: Select all
love:graphics:print(self:class:name, self:x, self:y)
Code: Select all
function love.graphics.print(...)
-- I don't use self here anyway
I'm talking about mere syntax here. Forgetting to write "local" in front of a local variable is just too easy.Robin wrote: P*thon does that, but Lua's way has two distinctive advantages:
- You have more control over what “local” means. (Inside this module? Inside this function? In this loop? Is it perhaps an upvalue?)
- Lua is a configuration language from origin, which means encapsulation is usually a bad thing.
How about:kikito wrote:That is not what I meant. Let me be a bit more precise: First, I only meant it to be used on method calls: love.graphics, self.class.name, self.x, self.y would work just the same as they do.
The only difference would be love.graphics.print. On that call, love.graphics would be sent to that function as a parameter called self. But you would be completely free to ignore it if you wanted to on your function definition:
The thing is, most of the time, when you do something.method() you do want to make something self. If you don't want to, you can safely ignore that extra self parameter. And for those weird cases when you really can't afford an extra parameter, the invocation could be done with :.Code: Select all
function love.graphics.print(...) -- I don't use self here anyway
Code: Select all
function foo()
-- what is 'self' here?
end
a={}
function a.foo()
end
foo = a.foo
foo() -- what is 'self' here?
return function () print(#self) end -- what is 'self' here?
That's not what I meant.kikito wrote:I'm talking about mere syntax here. Forgetting to write "local" in front of a local variable is just too easy.
I'd rather have a "global" keyword, and have locals defined by default. This would not only change the "configurationness" of the language in that you would have to write "global" in front of the configuration variables.
And you would still have the same level of control; still locals and globals. Just changed the way you declare them.
Code: Select all
function foobar()
i = 0 -- remember: local by default
for j=1,20 do
i = j + 1
end
print(i)
do
i = 'hi'
end
print(i)
end
Code: Select all
def f() print(self~=nil) end
a.f = f
f() --false
a.f() -- true
Code: Select all
c = C:new()
c:pushState('x')
c:pushState('y')
c:pushState('z')
Code: Select all
+---+ +---+ +---+
CLASS A | x | +--| y | +--| z |
+---+ | +---+ | +---+
Î | Î | Î
+---+ | +---+ | +---+
CLASS B | x | | | y | | | z |
+---+ | +---+ | +---+
Î | Î | Î
+---+ | +---+ | +---+
CLASS C | x |<-+ | y |<-+ | z | <-- We start looking for foo here
+---+ +---+ +---+
Code: Select all
classDict.__index = function(instance, methodName)
...
local stack = _private[instance].stateStack -- stack is a regular lua table
local method
for i = #stack,1,-1 do -- reversal loop
method = stack[i][methodName]
if(method~=nil) then return method end
end
end
--if not found, look on the class
return classDict[methodName]
end
Not as far as I can tell.kikito wrote:
- Any obvious deffects?
- Any possible optimization strategies?
Code: Select all
classDict.__index = function(instance, methodName)
...
local stack = _private[instance].stateStack -- stack is a regular lua table
for i = #stack,1,-1 do -- reversal loop
local method = stack[i][methodName] -- you don't need 'method' outside of this for loop.
if(method~=nil) then return method end -- also, if you don't care about false values, you can change this to 'if method then ...'
end
end
--if not found, look on the class
return classDict[methodName]
end