Posted 23 April 2012 - 11:51 PM
Pseudo Object Oriented Programming in ComputerCraft
Here's a generic example of a POOP (har, har) program for CC, which accepts a string, then underlines it with a definable character.
underline = { decor = "*" }
Underline is a table, which stores variables and functions. The first statement in bold above defines underline as a table, with a variable "decor", equal to an asterisk.
Reference a variable in a table with dot notation. For example, to reference the decor variable in our underline table/object use underline.decor
function underline:output( … )
Colon notation for referencing/adding functions in a table is table:function.
This helps with code legibility. Basically these two following pieces of code are the same thing:
Now the more legible/my preferred version:
Let's take a look at the arguments for output()…
function underline:output( … )
The ellipses in the function args denotes any arguments passed, in an index. SO if we print( … ) we print the args. If we args = { … } we put each arg as an element in an array/table.
underline:output( "your dog is fat." )
Obviously calling the predefined function output from the underline table/object, sending the string your dog is fat to be underlined with underline.decor.
Outputs:
self.decor
This is dot notation, where decor is a variable in the table self, where self automatically = the table containing decor.
caratUnderline = underline
Create a class of the object underline called caratUnderline, which basically loads a copy of the underline object, including its variables (decor) and functions (output).
caratUnderline.decor = "^"
Sets the underline decorator for our caratUnderline class to a carat.
caratUnderline:output( "your dog isn't so fat." )
Use the output function we copied from underline to caratUnderline, which also uses its decorator variable, since we defined self in output(). Prints the string, then underlines it with a carat.
Conclusion
To conclude with what I'd like to impress upon you, note these last lines:
This snippet adds another underline object, which will underline strings with an octothorpe. The last line does exactly that–print and underline with an octothorpe. So now we two different classes, so we can easily underline with a carat by using caratUnderline:output( "text" ), or hashUnderline:output( "text" ) to underline a string with an octothorpe, etc. You can always use the initial object just as you would a class.
Coming up next: object constructors, setmetatable.
Here's a generic example of a POOP (har, har) program for CC, which accepts a string, then underlines it with a definable character.
underline = {
decor = "*"
}
function underline:output( ... )
print( ... )
for i=1, #... do
io.write( self.decor )
end
print()
end
underline:output( "your dog is fat." )
caratUnderline = underline
caratUnderline.decor = "^"
caratUnderline:output( "your dog isn't so fat." )
hashUnderline = underline
hashUnderline.decor = "#"
hashUnderline:output( "JK, it has an eating problem!" )
underline = { decor = "*" }
Underline is a table, which stores variables and functions. The first statement in bold above defines underline as a table, with a variable "decor", equal to an asterisk.
Reference a variable in a table with dot notation. For example, to reference the decor variable in our underline table/object use underline.decor
function underline:output( … )
Colon notation for referencing/adding functions in a table is table:function.
This helps with code legibility. Basically these two following pieces of code are the same thing:
yourMom = { weight = 9001, isFat = function(self) return true end }
Edit: added self as arg, thanks for pointing that out AnrDaemon!Now the more legible/my preferred version:
yourMom = { weight = 9001 }
function yourMom:isFat()
return true
end
Let's take a look at the arguments for output()…
function underline:output( … )
The ellipses in the function args denotes any arguments passed, in an index. SO if we print( … ) we print the args. If we args = { … } we put each arg as an element in an array/table.
underline:output( "your dog is fat." )
Obviously calling the predefined function output from the underline table/object, sending the string your dog is fat to be underlined with underline.decor.
Outputs:
your dog is fat
***************
self.decor
This is dot notation, where decor is a variable in the table self, where self automatically = the table containing decor.
caratUnderline = underline
Create a class of the object underline called caratUnderline, which basically loads a copy of the underline object, including its variables (decor) and functions (output).
caratUnderline.decor = "^"
Sets the underline decorator for our caratUnderline class to a carat.
caratUnderline:output( "your dog isn't so fat." )
Use the output function we copied from underline to caratUnderline, which also uses its decorator variable, since we defined self in output(). Prints the string, then underlines it with a carat.
Conclusion
To conclude with what I'd like to impress upon you, note these last lines:
hashUnderline = underline
hashUnderline.decor = "#"
hashUnderline:output( "JK, it has an eating problem!" )
This snippet adds another underline object, which will underline strings with an octothorpe. The last line does exactly that–print and underline with an octothorpe. So now we two different classes, so we can easily underline with a carat by using caratUnderline:output( "text" ), or hashUnderline:output( "text" ) to underline a string with an octothorpe, etc. You can always use the initial object just as you would a class.
Coming up next: object constructors, setmetatable.