How to pass a parameter to a function reference?

I think that this is called a function reference but I’m not sure.

Anyway, this works:

def start_link(_arg) do
  Task.start_link(&check_updates/0)
end

But what if check_updates requires a parameter? So it would be check_updates/1. How can I call it in Tasks.start_link function?

fn () -> check_updates(whatever) end

4 Likes

Then you’d have to use an anonymous function instead:

def start_link(_arg) do
  Task.start_link(fn -> check_updates(arguments) end)
end

:slight_smile:

EDIT: Lol @sribe by like 2 seconds! ^.^

1 Like

:men_wrestling:

1 Like

To be picky &check_updates/1 is actually just syntactic sugar for fn (x) -> check_updates(x) end. All the & capture operator does is transform to an anonymous function. It has some syntax limitations when using it to include arguments, for example when you wanted to create an anonymous function with 0 arguments.

5 Likes