Skip to content
kurapica edited this page Jul 28, 2014 · 30 revisions

Pure lua object-oriented program system

PLoop provide an object-oriented program system with a special sugar syntax system. Several features contains in it:

  • Namespace system used to contains custom types.

  • Enum system used to define enumeration value types.

  • Struct system used to define structure value types.

  • Class system used to define object types with methods and properties settings, also with object events and meta-methods.

  • Interface system used to define interfaces for class objects.

  • 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 used to apply several definitions for class's constructor, method or meta-method with the same name, will make the config features easily to be defined.

Now, it only works for Lua 5.1. Since the getfenv, setfenv, newproxy api is removed from Lua 5.2, the system won't works on it now, or you can provide them in another way.

How to use

Use loadfile or require to load the PLoop.lua file in the folder. Then you can try the below code:

do
	-- 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"
end

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

do
	-- 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
end

Here I use do ... end because you may try those code in an interactive programming environment, many definitions like class, struct, interface's definitions should be kept in one piece. After here, I won't use do ... end again, but just remember to add it yourself.

If you define those features in a file, there is no need to use do ... end.

Features

There are some keywords that the system will released into the _G :

The namespace & import are used to control the namespace system used to store classes, interfaces, structs and enums.

The enum is used to define enum types.

The struct is used to start the definition of a struct type. A data of a struct type, is a normal table in lua, without metatable settings, the basic lua value types like string, number, thread, function, userdata, boolean are also defined in the PLoop system as struct types. The struct types are used to validate or create the values that follow the explicitly structure that defined in the struct types, like position on a cartesian coordinates, we only need values like { x = 13.4, y = 33 }.

The class is used to start the definition of a class. In an object-oriented system, the core part is the objects. One object should have methods that used to show what jobs the object can do, also should have properties to store the data used to mark the object's state. A class is an abstract from objects of the same type, it can contains the definition of the methods and properties so the object won't do these itself.

The interface is used to start the definition of an interface. Sometimes we may not know the true objects that our program will manipulate, or we want to manipulate objects from different classes, we only want to make sure the objects will have some features that our program needs, like objects have a Name property. So, the interface is bring in to provide such features. No objects can be created from an interface, the interface can be extended by classes or other interfaces.

The Module is used to start a standalone environment with version check, and make the development with the PLoop system more easily, like we don't need to write down full path of namespaces. This topic will be discussed at last.


=========================================================================>Namespace

Clone this wiki locally