I’m learning Julia and looking at the anonymous functions page: Functions · The Julia Language
Why does the anonymous function go as the first argument to get
when the doc says the default parameter for an undefined key is the 3rd arg?
Collections and Data Structures · The Julia Language
Julia has special syntax for passing anonymous functions as arguments to other functions. See Do-Block Syntax for Function Arguments.
When you write something like:
get(dict, key) do
x = foo * bar()
return x
end
Is the same as writing:
get(() -> begin
x = foo * bar()
return x
end, dict, key)
In short, do-block
syntax is a convenient way to pass an anonymous function as argument, making code look more elegant, but with this syntax, the anonymous function will always be inserted as the first argument.
So, in the case of get
, it has a method where the default
argument is the last, but also a method where the default
argument is the first where it should be a function, allowing you to call get
with the convenient do-block
syntax.
Thanks for the quick response!
Now I see in the get
doc how there’s a function signature with f::Union{Function, Type}
as the first argument.