Help with rectangle type

Here’s some of my code:

local RectangleLibrary = {}
RectangleLibrary.__index = RectangleLibrary

local CircleLibrary = {}
CircleLibrary.__index = CircleLibrary

type Rectangle = {
	StartingPoint:Vector2,
	EndingPoint:Vector2,
	Rotation:number
}

type Circle = {
	Center:Vector2,
	Radius:number
}

----------------------------------------------------------------------

-- Reurns a new rectangle object by directly setting its properties from the parameters
function RectangleLibrary.new(startingPoint:Vector2,endingPoint:Vector2,rotation:number):Rectangle
	local new:Rectangle = setmetatable({},RectangleLibrary)
	new.StartingPoint = startingPoint
	new.EndingPoint = endingPoint
	new.Rotation = rotation
	
	return new
end

-- Returns a new rectangle object based on a gui object
function RectangleLibrary.fromInstance(instance:GuiObject):Rectangle
	local new:Rectangle = setmetatable({},RectangleLibrary)
	
end

-- Sets the properties of a gui object based on the rectangle object's properties
function RectangleLibrary:ToInstance(instance:GuiObject)
end

-- Returns the center of the rectangle
function RectangleLibrary:GetCenter():Vector2
	local startingPoint:Vector2 = self.StartingPoint
	local endingPoint:Vector2 = self.EndingPoint
	
	return startingPoint:Lerp(endingPoint,0.5)
end

How do I make the Rectangle type include all those functions without manually adding them?

Like this?

local RectangleLibrary = {}
RectangleLibrary.__index = RectangleLibrary

local CircleLibrary = {}
CircleLibrary.__index = CircleLibrary

type Rectangle = {
	StartingPoint:Vector2,
	EndingPoint:Vector2,
	Rotation:number
}

type Circle = {
	Center:Vector2,
	Radius:number
}

type Rectangle__index = typeof(setmetatable({}::Rectangle,CircleLibrary))

----------------------------------------------------------------------


-- Returns a new rectangle object based on a gui object
function RectangleLibrary.fromInstance(instance:GuiObject):Rectangle
	local new:Rectangle = setmetatable({},RectangleLibrary)

end

-- Sets the properties of a gui object based on the rectangle object's properties
function RectangleLibrary:ToInstance(instance:GuiObject)
end

-- Returns the center of the rectangle
function RectangleLibrary:GetCenter():Vector2
	local startingPoint:Vector2 = self.StartingPoint
	local endingPoint:Vector2 = self.EndingPoint

	return startingPoint:Lerp(endingPoint,0.5)
end

-- Reurns a new rectangle object by directly setting its properties from the parameters
return function(startingPoint:Vector2,endingPoint:Vector2,rotation:number):Rectangle__index
	return setmetatable({
		StartingPoint = startingPoint;
		EndingPoint = endingPoint;
		Rotation = rotation
	}::Rectangle,CircleLibrary)::Rectangle__index
end

Thanks, this is what I wrote:

type RectangleBase = {
	StartingPoint:Vector2,
	EndingPoint:Vector2,
	Rotation:number
}

type Rectangle = typeof(setmetatable({}::RectangleBase,RectangleLibrary))

type CircleBase = {
	Center:Vector2,
	Radius:number
}

type Circle = typeof(setmetatable({}::CircleBase,CircleLibrary))

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.