Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict Object-Oriented Method Overloading #1557

Open
scar2056 opened this issue Dec 6, 2024 · 0 comments
Open

Strict Object-Oriented Method Overloading #1557

scar2056 opened this issue Dec 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@scar2056
Copy link

scar2056 commented Dec 6, 2024

Implementation:

--!strict

local ClassName = {}
ClassName.__index = ClassName

function ClassName.new()
	local self = setmetatable({}, ClassName)
	self._privateProperty = nil :: Vector3?
	return self
end

type ClassName = typeof(ClassName.new())

-- doc comment vector2
function ClassName.OverloadVector2(self: ClassName, value: Vector2): Vector2
	return value
end

-- doc comment vector3
function ClassName.OverloadVector3(self: ClassName, value: Vector3): Vector3
	return value
end

ClassName.Overload = function(self: ClassName, arg1: any): any
	if typeof(arg1) == "Vector2" then
		return self:OverloadVector2(arg1)
	elseif typeof(arg1) == "Vector3" then
		return self:OverloadVector3(arg1)
	end
	error("Arguments did not match any overload.")
end
:: typeof(ClassName.OverloadVector2)
 & typeof(ClassName.OverloadVector3)

Usage 1:

local className = ClassName.new()

local raycastResult = workspace:Raycast(Vector3.new(), Vector3.new())

if raycastResult then
	local v3r = className:Overload(raycastResult.Position)
	Vector3.new():Lerp(v3r, 1);
end

Type Error: Type 'Vector2' could not be converted into 'Vector3'

Replacing raycastResult.Position with raycastResult.Position :: Vector3 removes the error, but should not be necessary. Hovering the cursor over v3r displays the type as Vector3 regardless of whether this casting is done.

When an overload is called with a colon syntax, placing the I-cursor inside the function will always display the first overload.

@scar2056 scar2056 added the bug Something isn't working label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

1 participant