Skip to content
kurapica edited this page Nov 30, 2015 · 30 revisions

Pure lua object-oriented program system

The main features in PLoop are those:

  • Namespace used to organize custom types and make them unique. Within it, you can define any namespace, enum, struct, class or _interface.

  • Enum used to define enumeration value types.

  • Struct used to define structure value types. Within it, you can define any enum, struct, class or _interface.

  • Class used to define object types with methods and properties settings, also with object events and meta-methods. Within it, you can define any enum, struct, class or _interface.

  • Interface used to define interfaces for class objects. Within it, you can define any enum, struct, class or _interface.

  • Attribute system used to give descriptions for every parts of the system: enum, struct, class, interface, method, property, event. Those are useful for frameworks and adding system features in a good manner.

  • Overload system is built on the attribute system, used to apply several definitions for class's constructor, method or meta-method with the same name.

Install

Download the zip-file, extract it and rename it to PLoop, move it in your LUA_PATH folder.

How to use

Use require "PLoop" to load the system. Then you can try the below code in Lua 5.1 (if you run the code in an interactive programming environment, wrap the code with do ... end) :

require "PLoop"

-- Start the class's definition
class "MyClass"
	-- Property Definition
	property "Name" { Type = System.String }

	-- Method Definition
	function Greet(self, name)
		print("Hello " .. name .. ", My name is " .. self.Name)
	end
-- End the class's definition
endclass "MyClass"

If your lua version is lua 5.2 or 5.3 you may use code :

require "PLoop"

-- Start the class's definition
class "MyClass" (function(_ENV)
	-- Property Definition
	property "Name" { Type = System.String }

	-- Method Definition
	function Greet(self, name)
		print("Hello " .. name .. ", My name is " .. self.Name)
	end
-- End the class's definition
end)

In other examples, I'll only use the first code format, you can convert it to the second code format, just by wrapping the code with a function.

Now we create a class with a Greet method and a Name property. Then we can use it like :

-- Create the class's object, also with init settings, 
-- the init table can contains properties settings and object event handlers
ob = MyClass { Name = "Kurapica" }

-- Call the object's method
-- Output : Hello Ann, My name is Kurapica
ob:Greet ( "Ann" )

-- Property settings, 123 is not a string, so 
-- Error : Name must be a string, got number.
ob.Name = 123
Clone this wiki locally