# How is my code selecting dispatch for constructor?

**URL:** https://discourse.julialang.org/t/how-is-my-code-selecting-dispatch-for-constructor/47442
**Category:** General Usage
**Created:** [September 28, 2020, 9:57pm UTC](https://discourse.julialang.org/t/how-is-my-code-selecting-dispatch-for-constructor/47442 "2020-09-28T21:57:48Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Aroeira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aroeira/32/22593_2.png) [@Aroeira](https://discourse.julialang.org/u/Aroeira)
#### Post date: [September 28, 2020, 9:57pm UTC](https://discourse.julialang.org/t/how-is-my-code-selecting-dispatch-for-constructor/47442/1 "2020-09-28T21:57:48Z")

</div>

Hello, I need some help in understanding how Julia is selecting the constructor for my struct.

Here is a brief context: I’ve been trying to think of ways to reduce the number of methods I have to code for a somewhat complex struct. So I decided to try something like `f(x...)` where I would try to accommodate various different ways to construct `f`.

A minimal example building an object `foo`

```julia
struct foo
    val::Float64
end

function foo(x...)
    println("Computing...")
    foo(prod(x))
end

```

This works fine, although I am not sure if it’s the most efficient way to do it or if it is fragile:

```julia
julia> foo(1.0)
foo(1.0)

julia> foo(1.0, 2.0, 3.0)
Computing...
foo(6.0)

```

My real code, however, uses parametric constructors. When I combine those things the error shows up:

```julia
struct bar{T}
    val::T
end

function bar{T}(x...) where T <: AbstractFloat
    println("Computing...")
    bar(prod(x))
end

```

Trying

```julia
julia> bar(1.0)

```

The function calls itself every time resulting in stack overflow.

My understanding is that Julia tries to find the most specific dispatch, why does it work in the first case but not in the second one?

Is `f(x...)` a bad idea in general?

Thank you!  
Gustavo.

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [September 29, 2020, 12:14am UTC](https://discourse.julialang.org/t/how-is-my-code-selecting-dispatch-for-constructor/47442/2 "2020-09-29T00:14:32Z")

</div>

The reason there is a difference is:

```julia
julia> methods(foo)
# 3 methods for generic function "(::Type)":
[1] foo(val::Float64) in Main at REPL[1]:2
[2] foo(val) in Main at REPL[1]:2
[3] foo(x...) in Main at REPL[2]:2

julia> methods(bar)
# 1 method for generic function "(::Type)":
[1] (::Type{bar})(val::T) where T in Main at REPL[3]:2

```

`function bar{T}(x...)` overwrote the constructor for `bar`. The following works just fine, because it leaves the `bar{T}` constructor alone.

```julia
function bar(x...)
    println("Computing...")
    bar(prod(x))
end

julia> bar(1.0)
bar{Float64}(1.0)

julia> bar(1.0, 2.0)
Computing...
bar{Float64}(2.0)

```

> [@Aroeira](#):
>
> Is `f(x...)` a bad idea in general?

Not in general, but sometimes…
