A new object-oriented programming idiom, and why you should ditch the old one!

This is a great solution i really never found something this nice for inheritence, however i can’t seem to access methods in the super class from the sub class or the server script that creates an instance of the sub class:
Super Class:

local AI = {}
AI.interface = {}
AI.schema = {}
AI.metatable = {__index = AI.schema}

local errorSignature = "{AI 2.0} - ERROR : "

-- Local Functions --
local function clearTable(table)
	for k in pairs (table) do
		table [k] = nil
	end
end

-- Constructor --
function AI.prototype(self)
	-- Physical Propieties --
	self.Rig = nil
	self.Humanoid = nil
	self.PrimaryPart = nil

	return self
end

function AI.interface.new()
	return setmetatable(AI.prototype({}),AI.metatable)
end

-- Class Functions --
function AI.schema.isDead(self: AI)
	return self.Humanoid.Health <= 0
end

type AI = typeof(AI.prototype()) & typeof(AI.schema)
export type Type = AI

return AI

Sub Class:

local ServerScriptService = game:GetService("ServerScriptService")
local ServerPackage = ServerScriptService.ServerPackage
local TiltAt = require(ServerPackage.Entities.TiltAt)

local AI = require(script.Parent)
local E_AI = {}
E_AI.interface = {}
E_AI.schema = {}
E_AI.metatable = {__index = E_AI.schema}

local errorSignature = "{Entity AI 2.0} - ERROR : "
local warnSignature =  "{Entity AI 2.0} - WARN : "

function E_AI.prototype(self,Rig:Model,Stats)
	-- Physical Propieties --
	self.Rig = Rig or error(errorSignature.."A Rig is required to use the AI Class.")
	self.Humanoid = self.Rig:FindFirstChild("Humanoid") or error(errorSignature.."The Rig must have an Humanoid.")
	self.PrimaryPart = Rig.PrimaryPart
	self.Head = self.Rig:FindFirstChild("Head") or nil
	
	return self
end

function E_AI.interface.new(Rig:Model,Stats) 
	local self = setmetatable(AI.interface.new(),E_AI.metatable)
	E_AI.prototype(self,Rig,Stats)
	return self :: E_AI
end

function E_AI.schema.MoveTo(self: E_AI,position: Vector3)
	print(self:isDead())
	self.Humanoid:MoveTo(position)
end


type E_AI = AI.Type & typeof(E_AI.prototype()) & typeof(E_AI.schema)

return E_AI.interface

Script:

local AI = EntityAIClass.new(workspace.Entities.Joe,{})
print(AI:MoveTo(Vector3.new(0,0,0)))
print(AI.isDead())

Both method calls print out an error:
isDead

Sorry if this is such a goofy question but how does your text editor look so insane?

To me it looks like Microsoft Visual Studios IDE so I don’t think it is Studios script editor

you forgot to do setmetatable(E_AI.schema, AI.metatable) in class E_AI. and you must also call isDead with a colon print(AI:isDead()).

1 Like

Regarding the main topic, the problems you point out are clearly of the tools, the code editor, and not of the language or OOP itself.
The tools that support a language, usually have many more updates than a language, so their problems, in this case of autocompletion or suggestions, are usually solved over time.
So I don’t think “… you should ditch the old one!” is necessary. We should just wait for the tools to improve and support the most used OOP idiom.

1 Like