Is this way of type-checking possible?

Let’s say that we have a function that clones an instance in replicatedstorage and returns it. These are the function’s inputs and outputs.

Input: (instance_name: string) --the name of the instance to be copied
Output: (cloned_instance: Instance)

Is there a way to give the outputted instance a type that is somehow calculated properly? By that, I mean one instance in replicated_storage could hypothetically be a BasePart, but another is a TextButton. Is there a way to assign the correct types to the specific object through the output?

If anybody is confused, please let me know and I can try to rephrase.

Thank you.

1 Like
1 Like

Thanks for the reply. I think I used “type checking” wrongly. Let me try to use an example of how this would work.

local button = cloner.clone_instance("my_text_button")
button.Text = "Hello world" 

        ^
        |

The type checker gives “Text” as an optionable property since the cloned_instance output of the function somehow spits out the correct type. I’m really sorry, this is hard to explain.

Right now, I am stuck with having to put types in every variable here.

I’m wondering if there is a way for the function to do it automatically.

@TheLazyRaven

local button = cloner.clone_instance("my_text_button")
if button:IsA("TextButton") then
    button.Text = "Hello world"
end

If that’s what you mean.

1 Like

Sorry. I’m talking about Roblox’s autocomplete and type checker system and getting it to provide the proper properties to the output instance.

ooo no clue. Sorry
good luck sounds like a pain

1 Like

If this returns a TextButton, you could do:

local button: TextButton = cloner.clone_instance("my_text_button")
button.Text = "Hello world"

No, as cloning an Instance could return any Instance. Your function will have to return the broad type of Instance and then your code which calls it will have to refine or cast the result afterwards.

e.g

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function cloneReplicatedInstance(instanceName: string): Instance?
	local instance = ReplicatedStorage[instanceName]
	return if instance then instance:Clone() else nil
end

local newLight = cloneReplicatedInstance("Light") :: PointLight
1 Like

Its not possible as studio will not look at the arguments you provided when type checking, the best thing you could do is to have separate functions for all the types.

Yeah. I’m just trying to see if it can be done more automatedly. Also, what if the cloned instance has children?

If you want studio to be able to see the children of the instance you are trying to clone (here we are talking specifically if you are in studio editing, not in-game) you shouldn’t specify the type of the instance you are cloning, as when you do studio focuses only on that instance and ignores the children.
To add onto my other post, the closest to what you are trying to achieve would be something like:

cloner.clone_instance = {
	["my_text_button"] = function()
		local newInstance = script.TextButton:Clone()
		newInstance.Parent = script.Parent.ScreenGui
		return newInstance
	end,
	["my_text_label"] = function()
		local newInstance = script.TextLabel:Clone()
		newInstance.Parent = script.Parent.ScreenGui
		return newInstance
	end
}

local button = cloner.clone_instance["my_text_button"]()

This would work the way you want it to, however we are creating way too many functions for the simple task of cloning. To avoid using all those functions we could just have table of references:

cloner.instances = {
	["my_text_button"] = script.TextButton,
	["my_text_label"] = script.TextLabel
}

local button2 = cloner.instances["my_text_button"]:Clone()

However this wouldn’t be able to automatically parent the newly cloned instance somewhere, instead you would have to do it yourself. So simply put you can’t have both simpler process of cloning and not having to repeat yourself, as creating a function that will handle every instance removes the ability to type check, whilst other way you have to constantly type the same code.

2 Likes

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