function foo(a::Int, b::Int, c::Int)::Int
return a + b +c
end
Is there a way to say all of a, b and c are Ints instead of repeating? Or is the “right” way to put the arguments in an array and pass the function that instead?
Edit: Ops, completely wrong section at first sorry.
If you just want to avoid repetition, you can use a type parameter restricted to a concrete type, eg
function foo(a::T, b::T, c::T)::T where {T <: Int}
return a + b +c
end
But (as we are in First steps), recall that there is no advantage from declaring types for reasons other than dispatch (broadly). So most experience users of Julia would write
Good point. Although I still find it slightly confusing when types matter and when they don’t. You haven’t seen the last of me here in first steps to put it like that.
Types in function arguments are generally not related to performance. Think of them as a filter, saying which methods are called for which arguments.
In your example above, your type declarations have zero impact on performance, they just limit your foo to Int arguments. And your return-type declaration is redundant because the compiler can already infer it.
(The whole point of Julia is that you can write type-generic code that is still fast.)
Thanks guys, helpful tips. I find it better to go a bit overboard at first, since I’m not used to thinking about types at all. My specialty is slow matlab code with bugs in it so this is new stuff for me. It gives me a sense of accomplishment knowing my bug at least is an Int now.