Overview
@generic adds type parameters to a function. GLuaLS uses them to carry types through helpers such as filter, map, and wrapper functions.
Syntax
Basic generic function
Multiple type parameters
Generic with constraint
Use: ConstraintType to restrict the types that T accepts:
Generic arrays
When to use generics
Generics work best for:- Functions that transform a value but keep its type
- Functions that filter or map over a typed list
- Functions that take a callback and return its result type
any can be easier to read.
String-template capture (GLuaLS)
Use backticks when a string argument names a class or type. Use this when a function takes a class name as text and returns that class type:Tuses the normal Lua value type ("x"becomesstring)`T`uses the string text as a class or type name ("sent_npc"becomessent_npc)