Kruxigt
1
Hi good folks!
Quick question which I’m sure has been answered many times but which’s answers I can’t find.
Can I specify a subtype for an optional argument. Something similar to:
function f(a::Int64, b::T=10) where T<: MyOwnSpecialInt
If I can, what is the proper syntax? If not, why, and what is the proper way to do it instead?
That syntax works fine:
julia> f(a::Int, b::T=2) where T <: Real = a + b
f (generic function with 2 methods)
julia> f(1,2)
3
julia> f(1,2.0)
3.0
julia> f(1,2.0f1)
21.0f0
What error are you getting?
2 Likes
Kruxigt
3
Thanks! I just realized my error.
In actuality I was trying to do
function f(a::Int64, b::T=OneOfMySpecialInts) where T<: MyOwnSpecialInt
But of course it should be
function f(a::Int64, b::Type{T}=OneOfMySpecialInts) where T<: MyOwnSpecialInt
Kind of classic mistake for me! While I love the multiple dispatch I’m not always friend with the syntax.
1 Like