Generic Functions

This topic guides you what are generic functions in luau

Recommended for experienced programmers that understands basic type checking or more


Introduction

Summary:

  • What are generic functions? - Generic functions lets you influence how types of your function’s arguments and return type are to be inferred, for example: a generic function’s all arguments expected types in its function call are inferred to be string instead of any or unknown
  • When should i use them? - When you want a function to accept anything with intellisense of the real type intact
  • How do i use and create them? - explained below

Generic Functions

Creating a generic function is simple, You insert an angle brackets <> between the function’s name and its parameters and fill it in a tuple of names (it is not compulsory for them to be uniform, note that tuples are allowed to have a single data) which denotes placeholder types (like unassigned variables) that are later to be inferred to an argument’s type if type annotated to it

You would wanna read the comments in this code because it carefully explains generic functions
Also a “placeholder type” can be referred to as a Generic or a Type parameter

--!strict
local function example<A>(arg1: A, arg2: A, arg3: A): (A, A, A)
	return arg1, arg2, arg3
end

--[[
placeholder type "A" is now inferred to be string since the first argument
type annotated to this placeholder type is a string and so arg2 and arg3 is
type annotated to be string functionally because arg1 was a string

if one argument other than arg1 has a different datatype than arg1
the placeholder type will become a union type consisting of those
datatypes, for example if arg2 was a number instead of a string
and arg1 was a string, the type would be "number | string"

Using strict mode in generic functions might be advised
]]
local result1, result2, result3 = example("string1", "string2", "string3")

note again there can be more than placeholder types and the placeholder types names dont need to be uniform as long as its a valid tuple


Generic Type Instantiation

Generic Type Instantiation lets you explicitly fill out or slot types for these placeholder types in advance and so the placeholder type cannot alter the type it holds

To conduct this you insert double angle brackets <<>> between the function name and parameters and fill it with a tuple consisting of types matching with the placeholder types (for example: the first type in the tuple will make the first placeholder type to hold that type)

I advise you read the comments for a more comprehensible context

--!strict
local function example<A, B>(arg1: A, arg2: A, arg3: B, arg4: B)
	
end

--[[
placeholder Type A is now type "string" and placeholder Type B is now
type "number"
]]
example<<string, number>>("string1", "string2", "string3", "string4") -- invalid because arg3 and arg4 is supposed to be a number

Conclusion

Overview:

  • Generic functions - Influence how types of arguments and return type are to be inferred
  • Generics - Simply a placeholder type that is handled automatically by type inference or explicitly instantiated

Altogether, Generics helps code to be dynamic

Strongly appreciate any honest feedback or suggestions or questions about this guide

otherwise vote in this poll

  • Bad tutorial
  • Normal tutorial
  • Good Tutorial
0 voters
1 Like