I have seen functions defined without argument values, just types for dispatch as so:
function foo(::Int, n)
return Int(n)
end
First question: What is this type-only argument style called? I can’t seem to find its documentation.
Second question: Is there a way to call such a function without passing a value for the first argument? In principle, something like foo(::Int, 4) could be possible (but I’m not sure if it’s supported or considered somehow unjulian).
Just means that you won’t use the value of the argument in your function. It’s otherwise the same thing as f(x::Int). If you want a function that accepts a type object, not an instance, you write g(T::Type{Int}) (or indeed g(::Type{int})).
Aha, so I’m guessing I need to bite the bullet and refactor a little like,
function foo(type, n)
type==Int && return Int(n)
end
function foo(::Int, n)
return foo(Int, n)
end
Thanks! Also thanks to @nsajko for the reference to Holy Traits. I’ve seen references to this before but now might be a good time to dig into it and see if they can help me in my current project.
If you want to test your program, you’d generate a random value for the first argument.
I don’t think it a good idea to allow passing in both Int itself and its instances.