Skip to content

Instantly share code, notes, and snippets.

@octacian
Last active April 1, 2018 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octacian/c2817a2c934b42fe2ddc3163719941d5 to your computer and use it in GitHub Desktop.
Save octacian/c2817a2c934b42fe2ddc3163719941d5 to your computer and use it in GitHub Desktop.
My thoughts on Lua: what is missing and what should be changed

This documents the features and adjustments that I would personally like to see integrated with Lua, although this amount of tweaking and additions could likely make a project implementing them to be considered an entirely different language. The affect on performance and feasibility of these suggested features should be carefully considered previous to beginning the implementation of any.

Missing Features

Changes

  • Use slashes for comments
  • Change starting array index to 0
  • Use + instead of .. to concatenate strings (consistency, cleaner, makes sense when considered similar to operator overloading)
  • str() instead of tostring() and int/num() instead of tonumber()
  • Change tables to be pass-by-value rather than pass-by-reference (a table passed to a function which modifies it currently affects the original variable containing the table, unless a copy of the table is first created using table.copy)

Examples

Note: Suggested implementations for several of the crossed out features listed above can still be found below. Not only that, but any line of code that is completely commented without explanation is equal to that of "crossed out" items above.

Class Syntax

class example
	construct(var)
		self.variable = var -- public member variable
		self._private = "hello" -- private member variable
	end
	
	destruct() -- optional
		self = nil -- example
	end
	
	function aMethod(var)
		self._private = var
	end
	
	function _privateFunc(var) -- example private function
		self._private = var
	end
	
	string() -- defines what happens when the object variable is printed by itself (e.g. print(newInstance))
		return self.variable .. ": " .. self._private
	end
end

class two extends example -- inherits public and private data from `example`
	construct(var, another) -- overriding the constructor
		self.variable = var
		self._private = another
	end
end

local newInstance = two("hello", "world")

Operator Overloading (Integrated With Classes)

class example
	construct(x, y, z)
		self.x = x
		self.y = y
		self.z = z
	end
	
	+(otherExample)
		return example(self.x + otherExample.x, self.y + otherExample.y, self.z + otherExample.z)
	end
end

local first = example(1, -1, 4)
local second = example(2, 3, 1)
local third = first + second -- x: 3, y: 2, z: 5

Improved Scope Syntax

local lVar = 1 -- defines a local variable
global gVar = 1 -- defines a global variable
lVar = 2 -- changes an existing variable
nVar = 0 -- errors, this syntax cannot create a new variable but only modify an existing variable

Promises

function hello()
	return Promise(function(resolve)
		resolve(true) -- Return true
	end)
end

hello().then(function(value)
	print("Received: " .. tostring(value))
end)

Switch Syntax

switch num
	if 0 then
		print("0")
		continue -- continue to next check
	end
	if < 10 then
		print("less than 10") -- automatically break
	end
	if < 5 then
		print("less than 5")
	end
end

Likeness of Ternary Operator

local bool = false
local var = if bool == false then "it is false" else "it is true" end
var = if var == "it is true" then true elseif var == "it is false" then false end

Pointer/Reference Syntax

local variable = "Hello!"
print(variable) -- Prints "Hello!"
local pointer = *variable
pointer = "Changed by pointer."
print(variable) -- Prints "Changed by pointer."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment