LuaU how to typecheck as function

Let’s say I want to pass a function as a parameter for another function

function example(parameter)
    parameter()
end

How would I write this in LuaU? If parameter were a number it would look something like this

function example(parameter : number)
    print(parameter)
end

So if I want parameter to be a reference to a function I should just type function instead of number like this, right?

function example(parameter : function)
    parameter()
end

Well, when I try this I get a red underline that displays an error when highlighted
Syntax error: Expected type, got 'function'
I get this same error if I try to run the code anyway. So how do I do this?

Thanks in advance.

function example(parameter: () -> nil)
    parameter()
end

or

example: (parameter: () -> nil) -> nil = function(parameter)

end

and you have types for the function arguments.

4 Likes

i do

function feed(func)
  if (type(func) ~= "function") then return print("that is not a function") end;
  func("super idol")
end

feed(print)

it’s better than waiting a browser tab opening

Wow, thanks for responding so fast. This is exactly what I was looking for. A lot of this LuaU stuff isn’t very easy to google so it’s sure going to be a learning curve.