Function Annotations
Function annotations serve a similar purpose to type annotations, in that they tell the compiler what type of function to assign to this variable. This can be done either by assigning the type to a local variable or in the function itself.
local f: (number, string): boolean
-- Valid assignment
f = function(a: number, b: string): boolean
return true
end
The code above demonstrates how to utilize function annotations correctly. Note that primitive types are interchangeable. If the types of functions are not identical to the definition, then the compiler will throw an exception. Below is another example of how to annotate a function with types:
local function f(a: number, b: number): boolean
return a == b
end
-- Error: should return boolean not string
local function g(x: number): boolean
return "Hello"
end
Last updated
Was this helpful?