# Optional arguments of subtype

**URL:** <https://discourse.julialang.org/t/optional-arguments-of-subtype/71333>\
**Category:** General Usage\
**Created:** [November 11, 2021, 2:43pm UTC](https://discourse.julialang.org/t/optional-arguments-of-subtype/71333 "2021-11-11T14:43:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kruxigt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kruxigt/32/11694_2.png) [@Kruxigt](https://discourse.julialang.org/u/Kruxigt)\
**Post date:** [November 11, 2021, 2:43pm UTC](https://discourse.julialang.org/t/optional-arguments-of-subtype/71333/1 "2021-11-11T14:43:08Z")

</div>

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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 11, 2021, 2:46pm UTC](https://discourse.julialang.org/t/optional-arguments-of-subtype/71333/2 "2021-11-11T14:46:38Z")

</div>

That syntax works fine:

```julia
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?

---

<div class="post-metadata">

**Author:** ![Kruxigt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kruxigt/32/11694_2.png) [@Kruxigt](https://discourse.julialang.org/u/Kruxigt)\
**Post date:** [November 11, 2021, 2:55pm UTC](https://discourse.julialang.org/t/optional-arguments-of-subtype/71333/3 "2021-11-11T14:55:12Z")

</div>

Thanks! I just realized my error.  
In actuality I was trying to do

```julia
function f(a::Int64, b::T=OneOfMySpecialInts) where T<: MyOwnSpecialInt

```

But of course it should be

```julia
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. 🙂
