Page 21 of 25

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Fri Oct 14, 2011 1:52 pm
by TechnoCat
It prints

Code: Select all

true
It works as a global though.

EDIT: I haven't tested it because I'm in class, but I bet it will work if I require inside of the entered callback instead of on a file scope level.
EDIT2: Nope. Also didn't make any sense when I put it in code. :neko:

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Fri Oct 14, 2011 3:43 pm
by kikito
Hi technocat,

I've found the problem.

You had this code inside: lua/state/Menu.lua:

Code: Select all

local Shmup = require 'lua.shmup'
However, lua.shmup was not a file; it was a directory - lua/shmup/ . When LÖVE requires a folder, it automatically tries to append "init.lua" to that folder. Which is what happened here - the line above was in reality doing Shmup = require 'lua.shmup.init'

But the init.lua file was not returning the Shmup class:

Code: Select all

local _path = ({...})[1]:gsub("%.init", "")

return require( _path .. '.Shmup' )
Hence the error. I'm still a bit surprised that the require returned "true" instead of "nil" though.

If you change init.lua to return the Shmup class, it works (attaching working version) but since you are going to include more classes inside the shmup folder, I'd either return a table called shmup containing all of them (shmup.Shmup, shmup.Foo, etc) or just make them global and be done with it.

Regards!

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Fri Oct 14, 2011 4:19 pm
by slime
About require returning true rather than nil:
Once a loader is found, require calls the loader with a single argument, modname. If the loader returns any value, require assigns the returned value to package.loaded[modname]. If the loader returns no value and has not assigned any value to package.loaded[modname], then require assigns true to this entry. In any case, require returns the final value of package.loaded[modname].

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Fri Oct 14, 2011 4:29 pm
by kikito
Interesting. The more you know...

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Mon Jan 16, 2012 12:05 pm
by Indigo gem
Is it invalid use to call super class initialize like this?

Test1 = class("Test1")
Test2 = class("Test2", Test1)
Test3 = class("Test3", Test2)

function Test3:initialize(params)
self.super.class.initialize(self, params) -- go in infinite recursion call
end

this works for Test1 -> Test2 but for Test1 -> Test2 -> Test3 not works because initialize go in infinite recursion call

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Mon Jan 16, 2012 12:45 pm
by kikito
Hi Indigo Gem,

I have just tried your code. I just made one change: Instead of

Code: Select all

self.super.class.initialize(self, params)
The class and super had to be switched:

Code: Select all

self.class.super.initialize(self, params)
Here's the complete code:

Code: Select all

require 'middleclass'

Test1 = class("Test1")
Test2 = class("Test2", Test1)
Test3 = class("Test3", Test2)

function Test3:initialize(params)
  self.class.super.initialize(self, params)
end

local obj = Test3:new()
This worked just fine - it run without giving me any errors or entering any infinite loop.

Is the code that you used here complete, or is there any missing stuff? Notably - are there definitions of Test1.initialize and/or Test2.initialize?

I'm asking because you are using this construction:

Code: Select all

self.class.super.initialize
If you used self.class.super inside the Test2 initializer, you will get an infinite loop if self is an instance of Test3. For instances of Test3, "self.class" is always Test3, and "self.class.super" is always Test2 ... even on methods inherited from Test2! In consequence, "self.class.super.initialize" always refers to "Test2.initialize". If you call it from itself, you get an infinite loop.

The only solution is not using "self.class.super", but using the class instead:

Code: Select all

Test2.initialize(self, params)
If the code you have pasted there is complete (there are no missing constructors, etc) then your version of middleclass is probably not updated. Try again with the latest version.

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Mon Jan 16, 2012 4:48 pm
by Indigo gem
here code I used, I admit I make a mistake when typing (dont write constructers etc.), but this is full code below dont work

Code: Select all

require "middleclass"

local Test1 = class("Test1")
local Test2 = class("Test2", Test1)
local Test3 = class("Test3", Test2)

function Test1:initialize(params)
end
function Test2:initialize(params)
self.class.super.initialize(self, params) 
end
function Test3:initialize(params)
self.class.super.initialize(self, params) -- go in infinite recursion call
end

local t = Test3{}
I am tested this in Corona SDK btw, (lua 5.1), here is log

Code: Select all

Copyright (C) 2009-2011  A n s c a ,  I n c .
	Version: 2.0.0
	Build: 2011.704
The file sandbox for this project is located at the following folder:
	(/Users/sergey/Library/Application Support/Corona Simulator/engine-2419A65E500758E2AE1AF33CF8CAA007)
Runtime error
	/Users/sergey/projects/engine/main.lua:50: stack overflow
stack traceback:
	[C]: ?
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	...
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:53: in function 'initialize'
	/Users/sergey/projects/engine/middleclass.lua:90: in function </Users/sergey/projects/engine/middleclass.lua:88>
	(tail call): ?
	/Users/sergey/projects/engine/main.lua:56: in main chunk
Runtime error: /Users/sergey/projects/engine/main.lua:50: stack overflow
stack traceback:
	[C]: ?
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	...
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:50: in function 'initialize'
	/Users/sergey/projects/engine/main.lua:53: in function 'initialize'
	/Users/sergey/projects/engine/middleclass.lua:90: in function </Users/sergey/projects/engine/middleclass.lua:88>
	(tail call): ?
	/Users/sergey/projects/engine/main.lua:56: in main chunk
so I'll just use explicit call of super class

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Mon Jan 16, 2012 4:55 pm
by kikito
Hi,

I'm pretty sure that the loop doesn't happen in Test3, but in Test2. But yes, use explicit classes instead of self.class and you should be fine. Also, show us what you end up doing, even if it's in Corona :) !

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Wed May 16, 2012 9:27 am
by Roland_Yonaba
Hi,

I was wondering about the use of Invokermixin, by itself.
Basically, it calls a method or a property of an instance.
But why would one do:

Code: Select all

obj:invoke('method',...)
instead of

Code: Select all

obj:method(...)
I don't see the point. Maybe you can provide me some examples ?
And by the way, I guess the error messange on checking MiddleClass should be corrected ('Please require it before using Invoker, not Beholder').

Re: middleclass & extras: middleclass 2.0 is out!

Posted: Wed May 16, 2012 3:48 pm
by TechnoCat
Roland_Yonaba wrote:Hi,

I was wondering about the use of Invokermixin, by itself.
Basically, it calls a method or a property of an instance.
But why would one do:

Code: Select all

obj:invoke('method',...)
instead of

Code: Select all

obj:method(...)
I don't see the point. Maybe you can provide me some examples ?
And by the way, I guess the error messange on checking MiddleClass should be corrected ('Please require it before using Invoker, not Beholder').
I'm guessing because with invoke you can pass it a variable string that gets invoked.