Generic functions

I’m having a little trouble with generic types
I have all the logic for types worked out except when it come to actually said types.
For example, in this case I have a function foo that takes a generic type:

local function foo<T>(bar: T) ... end

however when I try to call foo and assign T:

foo<string>("some string")

Roblox does not except this as valid code.
Am I doing something wrong with my syntax?

Perhaps something is going over my head here, but this is not something I have ever seen done?

You are correct that typechecking occurs using the : character, E.g. function abc(var: string) would allow you to get autocomplete suggestions (and make reading the code easier) for var, knowing that it is a string.

If bar is always a string, I would suggest setting it up as follows:

local function foo(bar: string) ... end

When calling the function you don’t need to specify the type so its just:

foo("some string")

I am using something like this and roblox properly compiles this code

function module.has<T>(a: Set<T>, v: T): boolean
	if a[v] then
           return true
    return false
end

This code is a little wrong, ill correct it now

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