Bart van Strien avatar Bart van Strien committed 3754a25

Extend class creation syntax

Comments (0)

Files changed (4)

-**Slither** is a class library for lua that mimics python's classes, at the moment it has two big exceptions:
+**Slither** is a class library for lua that mimics python's classes, at the moment it has one big exception:
 
 - The class name is specified as a string
-- The class name of the parent(s) is specified as strings
 
 This means that:
 
 
 becomes
 
-    class "Cake" ("Food") {
+    class "Cake" (Food) {
     }
 
 
 
 foods = {}
 
+-- Old-style inheritence
 class "foods.Cake" ("Food") {
 	eat = function(self)
 		print("LIES!")
 	end
 }
 
+-- New-style inheritence
+class "foods.Candy" (Food) {
+	eat = function(self)
+		print("The dentist frowns upon your actions!")
+	end
+}
+
 banana = Food("banana")
 chocolate = Food("chocolate")
 portal_cake = foods.Cake("Portal Cake")
+sugary_candy = foods.Candy("Sugary Candy")
 
 banana:eat()
 portal_cake:eat()
+sugary_candy:eat()
 
 chocolate_banana = banana + chocolate
 chocolate_banana:eat()
 		})
 end
 
-local function inheritance_handler(name, ...)
+local function inheritance_handler(set, name, ...)
 	local args = {...}
-	if #args == 1 and type(args[1]) == "table" then
-		local root_table, name = stringtotable(name)
-		root_table[name] = class_generator(name, {}, args[1])
-		return
+	local t = nil
+	if #args == 1 and type(args[1]) == "table" and not args[1].__class__ then
+		t = args[1]
+		args = {}
 	end
+
 	for i, v in ipairs(args) do
-		local t, name = stringtotable(v)
-		args[i] = t[name]
+		if type(v) == "string" then
+			local t, name = stringtotable(v)
+			args[i] = t[name]
+		end
 	end
-	return function(t)
-		local root_table, name = stringtotable(name)
-		root_table[name] = class_generator(name, args, t)
+
+	local func = function(t)
+		local class = class_generator(name, args, t)
+		if set then
+			local root_table, name = stringtotable(name)
+			root_table[name] = class
+		end
+		return class
+	end
+
+	if t then
+		return func(t)
+	else
+		return func
 	end
 end
 
 function class(name)
 	return function(...)
-		return inheritance_handler(name, ...)
+		return inheritance_handler(true, name, ...)
 	end
 end
 
 	assert(not class_generator)
 	assert(not inheritance_handler)
 end)
+
+Test("New-style inheritence", function()
+	class "TestBase" {
+		some_var = true,
+		some_method = function(self) return self.some_var end,
+	}
+	class "Test" (TestBase) {
+	}
+	t = Test()
+	assert(t.some_var)
+	assert(t:some_method())
+end)
+
+Test("Returning of class", function()
+	test = class "Test" {
+	}
+	assert(test == Test)
+end)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.