A guide to generics

This tutorial guides you on what are generics in luaU type checking

Recommended for experienced programmers who has sufficient knowledge about type checking especially type aliases


Generics

Generics are simply placeholder values you can slot in a type for it to hold and so those placeholder values can be used and treated directly as a type, they are used by functions and type aliases which the two serves distinct purposes

We can use these terms to point out certain things in this topic:

  • Generic functions - generics in functions
  • Generic type aliases - generics in type aliases
  • Type parameter - the individual placeholder value also referred to as Generics

Generic functions lets you influence how arguments types and returned values types to be inferred by luau’s type inference

to create a generic in a function you put <> directly after the function name or function itself (depending on how function is declared by a variable) and you put in a tuple inside <> defining how much type parameters to be there

Examples (look at each individual functions as those are examples):

local function exampleFunction<A>()

end

local example2 = function<A>()

end

local function example2<A, B, C, D>()

end

For generic functions you commonly dont slot the type for the type parameters, instead if the type parameters were to be type annotated directly on the function’s parameters, the function’s parameters that is the first one to be type annotated with the generic will define what type the generic would hold and so the type would be the argument’s type filling the parameter

Example:

local function example<A, B>(value1: A, value2: A, value3: B, value4: B)

end
-- type parameter A now holds the type string, same goes for value3 but holds numbers
example("string", "string", 1, 2)

If you really want to slot the type for the generics, that is called Generic type instantiation and you would do this

example<<string, number>>()

you put <<>> on the function call after the name then fill in the types of the generics and so those generics are now explicitly string and number

For generic functions, the generics types are defined for each function call not globally


Generic type aliases

A generic type alias is a kind of type aliases which has type parameters that when using the type alias you would have to explicitly fill them out

defining a generic in type aliases is simple, you would add <> directly after the type alias name

example:

type example<A, B> = A|B

--[[
the "example" type is now explicitly string or number as you filled the type parameters with those types
]]
local variable: example<string, number>

An ordinary generic type alias requires you to fill out the type parameters with types, to stop this we use Default types which if the type parameters are not filled out, it is automatically filled out with a predefined type and to do this you add an equal sign to a type parameter to assign what its type would be if it is not filled out

example:

type example<A=boolean> = A

--[[
the "example" type now becomes "boolean" type because the type parameter assigned
to a default type was not filled and so the type becomes "boolean"

other info: "example" type becomes boolean because i assigned the type parameter directly
to the type alias itself
]]
local variable: example

the type parameters within <> regardless of generic function or generic type alias can always have more than one type parameter like it accepts a tuple

another example of default types:

type example1<A=string, B=boolean> = A|B
type example1<A=string, B=boolean, C=number> = A|B|C

--[[
"A|B" and "A|B|C" are just examples of using the generics
]]

Tutorial has ended

ask questions if confused
provide feedback or suggestions if necessary

if you think you have spotted a mistake in this tutorial please let me know about it