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

[CC][Question]Class Implementation

Started by Ditto8353, 19 October 2012 - 07:38 PM
Ditto8353 #1
Posted 19 October 2012 - 09:38 PM
I have been considering writing a tutorial on creating and using Classes in Lua, but I have a couple questions about CC's implementation.

1. Does CC support Single-Method class creation?
2.Once a Class is created in this way, in a separate file, say 'Point2D', should it be loaded with os.loadAPI() or require()

I feel as though I have a significant grasp on Class creation in Lua in order to write this tutorial. If you feel otherwise, please direct me to some reading material which you think may help.
faubiguy #2
Posted 19 October 2012 - 09:56 PM
1. I'm not exactly sure what you mean (not much experience with OOP), but if you want to create a class as in http://www.lua.org/pil/16.1.html with a single method, I see no reason why it wouldn't work.
2. CC doesn't have require. If you want to make your own require using dofile, you can do that, but using os.loadAPI is probably the best option.
Lyqyd #3
Posted 19 October 2012 - 09:58 PM
1. Not entirely sure what you mean by this. Clarify?
2. There is no require() in CC. os.loadAPI() does specific things, which I believe you can see in bios.lua. If this question is related to the first, it may require further clarification as well.

Have you taken a look at the vector "API"?
Ditto8353 #4
Posted 22 October 2012 - 01:07 PM
Single-Method class implementation:
//Written in a file named for the class you wish to create. (i.e. Car)
function newCar (make, model, color)
   local self = {make = make, model = model, color = color}
   local function getMake()
	  return self.make
   end
   local function getModel()
	  return self.model
   end
   local function getColor()
	  return self.color
   end
   local function paint(color)
	  self.color = color
   end
   return getMake, getModel, getColor, paint
end

I am hoping that this is supported by CC, simply because it is (as I see it) the simplest way to create a class in Lua so it would probably be easier for new programmers to understand.
Lyqyd #5
Posted 22 October 2012 - 03:47 PM
Yuck. You'd be much better off writing a tutorial on the style that the vector class included with ComputerCraft uses.
Ditto8353 #6
Posted 22 October 2012 - 03:53 PM
:3
Sounds good. Thank you for the input.