Question about OOP Classes

Hey again. So I wanted to make some feature that allows you to annotate functions, but I have no idea on how to do this. Basically, I am using a modified class module made by someone, and I want to add a way to annotate functions. Here is some example code of how the class module works:

local Class = require(game.ReplicatedStorage.Class)

local Vehicle = Class {
	constructor = function(self, maxfuel)
		self.Fuel = 100;
		self.MaxFuel = maxfuel;
	end,
	
	GetFuelLeft = function(self)
		return self.Fuel
	end,
}

local Car = Class(Vehicle) {
	constructor = function(self, maxfuel, brand)
		self.super(self, maxfuel);
		self.Brand = brand;
	end,
}

local Jeep = Car.new(250, "Jeep")
print(Jeep:GetFuelLeft()) --Would print "250".

However, I want to add a way to annotate table entries/functions, so you could do something like this in theory:

local Class = require(game.ReplicatedStorage.Class)

local Vehicle = Class {
	constructor = function(self, maxfuel)
		self.Fuel = 100;
		self.MaxFuel = maxfuel;
	end,
	
    @FuelLimiter(max = 100) --Obviously I know you cant do this, but this is just an example of what I wanted to show.
	GetFuelLeft = function(self)
		return self.Fuel
	end,
}

local Car = Class(Vehicle) {
	constructor = function(self, maxfuel, brand)
		self.super(self, maxfuel);
		self.Brand = brand;
	end,
}

local Jeep = Car.new(250, "Jeep")
print(Jeep:GetFuelLeft()) --Would print "100" because the theoretical "@FuelLimiter" annotation would do math.clamp to clamp it between 0 and 100.

I think you will just have to do these type of checks inside GetFuelLeft.

Roblox-TS might have a feature like this, but I’m not familiar enough to know.

1 Like

I was hoping I could avoid this, I just wanted to make a way to do it oustide of the function

I don’t think so.

There might be some hacky tricks you could use with Moonscript, or a dialect of moonscript, but I asked someone I knew and they said Moonscript does not have any function annotations that you are looking for, but they suggested using statics, or factories.

MoonScript is a language that transpiles to Lua code.

https://moonscript.org/reference/

I’m confused about what you’re after. You can use comments to say --Max is 100 and then in your code when you write to full you can do

self.Fuel = math.clamp(self.Fuel+addedFuel, 0, self.MaxFuel)
1 Like

He wants “annotations” that could be defined outside the function to define the bounds of the function without explicitly modifying the function.

He knows he could clamp the value inside the function, but that was not the point.

1 Like

Like what @DrKittyWaffles said, I know how to use math.clamp, I was just using that as an example on implementation for annotations, which would allow inputs aswell