LuaWeaver

wOOP 1.0 Docs

Feb 8th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.36 KB | None | 0 0
  1. --[[
  2. wOOP is a powerful yet elegant Object Oriented Programming library. Class setup is nice as is instancing objects.
  3. In fact, wOOP was written because other libraries (in the opinion of the creator of wOOP) were "ugly" code.
  4. wOOP is based around userdata to support the __gc and __len metamethods. These functions do not work in tables in Lua 5.1.
  5. To create a class, you can do the following:
  6. ]]
  7. class "className" "extendsClass"
  8. {
  9.     property=15;
  10.     otherProperty=20;
  11.  
  12.     method=
  13.     function(a,b)
  14.         self.property=self.property+a
  15.         self.otherProperty=self.otherProperty-b
  16.     end;
  17.  
  18.     _readOnlyProperty=30;
  19.     __privateProperty="String";
  20.  
  21.     __settings=
  22.     {
  23.         new=
  24.         function(name)
  25.             return{name=name}
  26.         end;
  27.         mt=
  28.         {
  29.             __gc=
  30.             function(self)
  31.                 print(self.name.." is saying goodbye forever!");
  32.             end
  33.         }
  34.     }
  35. }
  36.  
  37. --[[
  38. You create a class by the name of "className", which inherits from the class "extendsClass".
  39. property and otherProperty are simply numbers stored inside the class.
  40. Notice that in "method" there is no self argument, but it uses one. This is because the "self" variable is "implied".
  41. You can use it in a function without explicitly declaring it as an argument.
  42. In wOOP, methods are called with a "." instead of a ":". (This is configurable).
  43. To create an object, simply call the class name as a function! The arguments will go into the new function.]]
  44.  
  45. local object=className("Joe")
  46. print(object.name,object.property)
  47. object.method(5,5)
  48.  
  49. --[[
  50. If you attempt to do any of the following, it will error. (This is configurable.)
  51. ]]
  52.  
  53. object.asdf="String" --Attempt to set "asdf", a nil value.
  54. object._asdf=30 --Attempt to set a read only value.
  55. print(object.__asdf) --Attempt to index "__asdf", a private value.
  56.  
  57. --[[
  58. Of course, the methods get direct access to the tables themselves.
  59. This means that inside method, the object can access itself.
  60. This is how private and read only values are useful in wOOP.
  61. To trigger the finalizer, simply do this:
  62. ]]
  63.  
  64. remove(object)
  65.  
  66. --[[
  67. This will remove and garbage collect the item. This is noticeable because "object" is now nil.
  68. ]]
  69.  
  70. print(object) --nil
  71.  
  72. --[[
  73. That covers the basic usage of wOOP. If you find any bugs/errors, please report them to LuaWeaver via Love2D forums.
  74.  
  75. Additional usage:
  76. Configuration table.]]
  77.  
  78. local config=
  79. {
  80.         imply_self=true; --Remove the need for setting a self value in functions (removes the need for a colon)
  81.         self_name="self"; --What the name is for the self value
  82.        
  83.         using_userdata=true; --Use userdata to allow __gc and __len metamethods. NOTE - WITHOUT THIS, STRICT ERRORS, PRIVATE, AND READ ONLY VALUES WILL NOT FUNCTION.
  84.        
  85.         strict_errors=true; --Error if 1) Value inside an object does not exist or 2) Attempt to set a value of an object where the field does not exist
  86.        
  87.         private_values=true; --Use private values
  88.         private_value_index="__"; --How to start private values
  89.        
  90.         read_only_values=true; --Use read only values
  91.         read_only_value_index="_"; --How to start read only values
  92.        
  93.         class_config_location="__settings"; --Where the config is located.
  94.         class_config_setup= --How the config is setup.
  95.         {
  96.                 new="new";
  97.                 mt="mt";
  98.         };
  99. }
  100.  
  101. --[[
  102. These should all be self explanatory. Simply modify the values to tweak it.
  103. ]]
Advertisement
Add Comment
Please, Sign In to add comment