This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
H4X0RZ's profile picture

Class loading failed

Started by H4X0RZ, 31 May 2014 - 01:32 PM
H4X0RZ #1
Posted 31 May 2014 - 03:32 PM
Hello community,
I'm working on a "class loading" thingy recently and it wont save what is in the function environment… :(/>
I don't get any real error, but nothing is added to the environment, but if I add for example a log function I can use it in my "class". Sorry if it's unclear what I want to say.

Here are the two files:
Loader.lua : http://pastebin.com/j6x3CT2a
The "class" for testing: http://pastebin.com/aZA1zv06

expected output:

loading fields of test
test has 3 fields
test = lol
func = function--some random stuff here
numb = 1

output I get:

loading fields of test
test has 0 fields

It would be amazing if someone could help me with that ^_^/>

~Freack100
Yevano #2
Posted 31 May 2014 - 03:59 PM
getfenv will return a table containing key-value pairs, the global table associated with that function. #env returns 0 because length is not calculated for the hash part of a table. I imagine cls.test.print wouldn't work either since you never defined a print function inside your test class.

Just remember:
assert(#{ a = 1, b = 2, c = 3 } == 0)
assert(#{ 1, 2, 3 } == 3)
assert(#{ 1, 2, 3, a = 1, b = 2, c = 3 } == 3)

And also, you just set cls[name] to an empty table. I'm guessing you probably want to set it to env instead. Of course, this will have the problem of seeing all its standard Lua globals, like print. (Or maybe that's what you want?) If you want to change that, you could do what SEE does and make the classes put all their methods and fields inside a table of the same name as the class. (link)


Test.someStaticVar = 1337
function Test.someStaticMethod()
	print("hi")
end
Edited on 31 May 2014 - 02:05 PM
SquidDev #3
Posted 31 May 2014 - 04:24 PM
If you want to change that, you could do what SEE does and make the classes put all their methods and fields inside a table of the same name as the class. (link)

Yeah, have a look at See. Have you read the Lua docs on Object-Oriented Programming?
Edited on 31 May 2014 - 02:59 PM
ardera #4
Posted 31 May 2014 - 05:39 PM
Of course, this will have the problem of seeing all its standard Lua globals, like print.
To let the program have access to the globals, just set the __index metamethod of the env to _G:

setmetatable(env, {__index = _G})
and then execute the file.
Yevano #5
Posted 31 May 2014 - 07:38 PM
Of course, this will have the problem of seeing all its standard Lua globals, like print.
To let the program have access to the globals, just set the __index metamethod of the env to _G:

setmetatable(env, {__index = _G})
and then execute the file.

That's not quite what I meant. I was meaning that the globals in the class might override the normal Lua globals. This is why it might be good to put the class methods and fields in their own table inside the class environment.
ardera #6
Posted 31 May 2014 - 08:34 PM
That's not quite what I meant. I was meaning that the globals in the class might override the normal Lua globals. This is why it might be good to put the class methods and fields in their own table inside the class environment.
Oh, yes, you're right.