# Dispatch surprise with vectors

**URL:** <https://discourse.julialang.org/t/dispatch-surprise-with-vectors/94214>\
**Category:** General Usage\
**Tags:** question, dispatch\
**Created:** [February 7, 2023, 2:26pm UTC](https://discourse.julialang.org/t/dispatch-surprise-with-vectors/94214 "2023-02-07T14:26:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [February 7, 2023, 2:26pm UTC](https://discourse.julialang.org/t/dispatch-surprise-with-vectors/94214/1 "2023-02-07T14:26:22Z")

</div>

I’ve been trying to implement some functions taking advantage of some default values as well as multiple dispatch and I stumbled upon the following behavior. It is surprising to me but maybe it is just the way things work in julia.

```julia
# Define a default const
const defn = [1,2,3]
# Define a function taking an abstract vector as argument and that returns it
fun1(numbers = defn::AbstractVector{<:Number}) = numbers

```

As expected calling `fun1()` returns

```julia
julia> fun1()
3-element Vector{Int64}:
 1
 2
 3

```

and `fun1([1,2,5])`

```julia
julia> fun1([1,2,5])
3-element Vector{Int64}:
 1
 2
 5

```

But the big surprise (to me at least) is that calling `fun1(10)` also works and returns

```julia
julia> fun1(10)
10

```

while `10 isa AbstractVector` gives

```julia
julia> 10 isa AbstractVector{<:Number}
false

```

Could anyone explain how this works, I would have expected a dispatch error saying that `fun1(::Int64)` does not exist. As a sidenote, this is exactly what happens if `fun1` has no default value…

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [February 7, 2023, 2:34pm UTC](https://discourse.julialang.org/t/dispatch-surprise-with-vectors/94214/2 "2023-02-07T14:34:11Z")

</div>

Both of these are valid but have different meaning:

```julia
fun1(x::AbstractVector{<:Number}=[1,2,3]) = x
fun2(x=[1,2,3]::AbstractVector{<:Number}) = x

```

You probably aimed for the former. The latter asserts that `[1, 2, 3]` (not the argument `x`) is an `AbstractVector{<:Number}`.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [February 7, 2023, 2:58pm UTC](https://discourse.julialang.org/t/dispatch-surprise-with-vectors/94214/3 "2023-02-07T14:58:09Z")

</div>

Well, needless to say that I completely overlooked that slight yet important difference, thanks for the help !
