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

Problems With OO Lua

Started by theoriginalbit, 11 January 2013 - 10:44 PM
theoriginalbit #1
Posted 11 January 2013 - 11:44 PM
hello all… it has come that time again that I need some fresh eyes having a look to see what I'm doing wrong…

I'm attempting to make my Extended String Library into an object and am running into problems.

thought id start it off easy with basic code, but it doesn't even work…

string library code:


str = {}

str.__index = str

str.__tostring = function( self ) return self.data end

function str.new( d )
  local stringObject = { data = ( d and d or "" ) }
  setmetatable( stringObject, str )
  return stringObject
end

how its being called:


os.loadAPI( "/Extended-String-Library/dirty/str")

local s = str:new()
print( tostring( s ) )

I get the error "tester:3: attempt to call nil"

if anyone can see my mistake that would be great :)/>
Orwell #2
Posted 11 January 2013 - 11:47 PM
You have no function new() in your API, only a function str.new(). So try 'graphics.str.new()' or make a wrapper for it? I wouldn't do it this way I think. Too bad you can't set the metatable of the API.

Edit: also, isn't:

d and d or ""
the same as

d or ""
? :P/>
theoriginalbit #3
Posted 11 January 2013 - 11:52 PM
You have no function new() in your API, only a function str.new(). So try 'graphics.str.new()' or make a wrapper for it? I wouldn't do it this way I think. Too bad you can't set the metatable of the API.

Edit: also, isn't:

d and d or ""
the same as

d or ""
? :P/>

oops copied the wrong test file… OP fixed…

very true… haha… that was old remnants that needed and or

EDIT: ahh I figured out the problem… took your suggestion and removed the str. from the front of the new declaration and added d as the second parameter in new… thanx :)/>
Edited on 11 January 2013 - 10:55 PM