#Need to know how type parameters work

1 messages · Page 1 of 1 (latest)

outer anvil
#

Have been beginning to strictly type check more and more, and I'm wondering, how do Type Parameters work?
How would you, say, define a function that make it's own function of which requires a type similar to how Signal+ does?

Below is my current experience with it; not even sure if it works ingame yet lol. but I know the type checking does
Feed me... INFORMATION...

--!strict

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Modules
local UtilitySignal = require(ReplicatedStorage.Modules.Utility.Signal)

-- Constants

-- Private Variables

-- Module
local Model = {}

type ChangedSignal = UtilitySignal.Signal<string, any>

export type ModelWrapper = {
    Connections : {[BasePart] : RBXScriptConnection},
    TransparencyNumbers : {[BasePart] : number},
    
    Changed : ChangedSignal,
    
    Model : Model,
    
    TransparencyMultiplier : number,
    
    Destroy : (ModelWrapper)->()
}

-- Initializing
function Model.new(templateModel : Model) : ModelWrapper
    
    local self = {}
    local proxy = {}
    
    setmetatable(self, {
        
        __index = function(t : ModelWrapper, index : any)
            
            if proxy[index] then
                return proxy[index]
            end
            
            return nil
            
        end,
        __newindex = function(t : ModelWrapper, index : any, v : any)
            
            if proxy[index] ~= v then
                proxy[index] = v
                self.Changed:Fire(index, v)
            end
            
        end,
    })
    
    self.Changed = UtilitySignal() :: ChangedSignal
    
    self.Model = templateModel:Clone()
    
    self.Connections = {}
    self.TransparencyNumbers = {}
    
    for i,v in pairs(self.Model:GetDescendants()) do
        if v:IsA("BasePart") then
            local v :BasePart = v
            local currentTransparency = v.Transparency or v:GetAttribute("Transparency")
            self.TransparencyNumbers[v] = currentTransparency
            self.Connections[v] = v:GetPropertyChangedSignal("Transparency"):Connect(function()
                if self.TransparencyNumbers[v] and self.TransparencyNumbers[v] ~= v.Transparency then
                    self.TransparencyNumbers[v] = v.Transparency
                end
            end)
        end
    end
    
    self.TransparencyMultiplier = 1
    
    function self.Destroy()
        for i,v in pairs(self.Connections) do
            v:Disconnect()
        end
        if self.Model then
            self.Model:Destroy()
        end
    end
    
    self.Changed:Connect(function(index : string, value : any) 
        if table.find({"TransparencyNumbers", "TransparencyMultiplier"}, index) then
            for i,v in pairs(self.TransparencyNumbers) do
                i.Transparency = v * self.TransparencyMultiplier
            end
        end
    end)
    
    return self
    
end

-- Returning
return Model
#

ignore how in newindex and index the index parameter is set to any, it's been changed to string

regal quartz
#

stop using metatables ✋

outer anvil
regal quartz
#

also I don't think u can do like

#

the thing u just said

outer anvil
#

Wdym ?

regal quartz
#

do u mean like

#

somefunction<T>(T something)
return function(T something)

end
end

#

do u mean like this

#

its not possible

#

is that what u mean tho

outer anvil
#

I mean like...

type AwesomeFunction<parameters, ...> = (parameters)->()

#

Setting function parameters to provided type parameters

regal quartz
#

oh so u put values in the type part

#

like those c++ thing?

#

the template<size_t t, typename T>

#

or im tripping

outer anvil
#

I don't know I've never used C++

regal quartz
#

ok

regal quartz
#

how do u put that to a function tho

#

it's like ur assigning a value to a type

#

oh

outer anvil
#

What if I made a signal that has type parameters, and I defined the type parameters you would need when firing it or subscribing/connecting to it. Would that be possible and how would i do it

regal quartz
#

wait so like

regal quartz
#

cast it to a function with a parameter

#

and the type will cast it to the parameter

#

ok I did some researching

#

u need to name it

#

like

#

type AwesomeFunction<parameters, ...> = (smth: parameters)->()

so u can cast it and then it'll also cast the parameter

outer anvil
#

I see

regal quartz
#

can u try it

outer anvil
#

Once I'm home! Currently at a restaurant