Classe<T> - Classes, OOP and Auto type annotations


creatorStore

github

wally


playground

:bookmark_tabs: What is Classe?

Classe is a smart class wrapper for Luau. It automatically calculates object structures, including fields and methods, using advanced type functions. Furthermore, Classe optimizes metadata lookups, ensuring high performance even with deep inheritance hierarchies

Classe handles all the heavy lifting for you: metatables, __index, and type casting. You no longer need to manually define every object field for type checking, the module infers and handles it automatically

:wrench: Class Example:

--!strict

local Gun = Classe.meta({ })

type Self = Classe.Self<typeof(Gun)>

function Gun.construct(self)
  self.bullets = 10
  return self
end

function Gun.shot(self: Self)
  self.bullets -= 1 -- OK!
end

return Classe.build(Gun)

:hammer_and_wrench: Inheritance Example:

--!strict

local Rifle, super = Classe.meta({}, Gun)

type Self = Classe.Self<typeof(Rifle)>

function Rifle.construct(self)
  self.range = 10
  super(self)

  return self
end

function Rifle.aim(self: Self)
  print("aiming...")
end

return Classe.build(Gun)

:gear: Creating an Object

local rifle = Rifle.new()

rifle:shot()
2 Likes

Wow amazing! @Yarik_superpro Check this out bruh!

3 Likes

this is just the equivalent of summoning mahoraga

@Yarik_superpro hahahahahhahahahahahshahahahahahahaahahahahahahahahahaahhahahahahah

Sorry for the necroposting but is there any support for static fields? Would be very nice to be able to define static fields.

1 Like

Add this field to the class metadata, and then it will behave like a static field

local meta = Classe.meta({ })

type Self = Classe.Self<typeof(meta)>

function meta.construct(self)
  self.some = "cool"
  return self
end

meta.staticField = 100 -- static field

local MyClass = Classe.build(meta)

--=====================

local object = MyClass.new()
print(object.staticField) -- 100

I expected static fields to not only work on instances but also on the classes themselves:

--...
local MyClass = Classe.build(meta)
local object = MyClass.new()

print(object.staticField) -- Should work
print(MyClass.staticField) -- Should also work

I didn’t implement metadata reading and autocompletion for this, but you can change the class metadata this way:

MyClass.staticField = 50

Here, __newindex is triggered, which modifies the metadata.