Help needed with Function Overloading

Title says it all.

The code below is located in my custom service and is meant to retrieve (either by finding or creating) a folder inside of a player.

I simply want the function to return a type of Folder or a type of Folder? depending on the parameter/ variable named “createIfNotFound” (boolean).

It is my understanding that lua does not have explicit function overloading like most other programming languages, but I would at least like to implement a workaround so that strict mode isn’t screaming at me when I use this function within my codebase.

I am new to strict typing, so any help is appreciated. Thanks

function EconomyService.getPlayerFolder(player: Player, folderName: playerFolderId, createIfNotFound: boolean?)
	
	local playerFolder = player:FindFirstChild(folderName) :: Folder?
	
	if type(createIfNotFound) == "boolean" then
		if createIfNotFound then
			if not playerFolder then
				playerFolder = Instance.new("Folder")
				playerFolder.Name = PLAYER_LEADERSTATS_FOLDER_NAME
				playerFolder.Parent = player
				return playerFolder
			end
		end
	end
	
	return playerFolder
end

not sure why you would want a get function to NOT return anything, when do you ever disable createIfNotFound? also just give up on strict typing because it has practically no advantages, best solution here is either just not changing this and leaving it be or making separate function

also is this not useless?

:x:

if (not (not createIfNotFound)) == true and playerFolder == nil then

:+1:

Personally, I find strict typing to be quite helpful.

For anyone who sees this post, the best ‘solution’ I came to is to hard-code the type whenever the aforementioned function is called with createIfNotFound = true.

Example:
local folder = EconomyService.getPlayerFolder(player, folderName, true) :: Folder

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