# dynamic Vararg count

**URL:** <https://discourse.julialang.org/t/dynamic-vararg-count/10144>\
**Category:** General Usage\
**Created:** [April 3, 2018, 10:48pm UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144 "2018-04-03T22:48:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)\
**Post date:** [April 3, 2018, 10:48pm UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/1 "2018-04-03T22:48:47Z")

</div>

The following code fails to compile (if n is not defined in Main that is). Is there a way to do something like this?

function f(n::Int); g(x::Vararg{Float64,n}) = sum(x); end

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [April 4, 2018, 12:22am UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/2 "2018-04-04T00:22:07Z")

</div>

If you want a version of sum that accepts any number of arguments try

```julia
Base.sum(xs...) = sum(xs)

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 4, 2018, 12:25am UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/3 "2018-04-04T00:25:28Z")

</div>

Probably shouldn’t make it a method of `Base.sum` though (type piracy).

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [April 4, 2018, 12:27am UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/4 "2018-04-04T00:27:39Z")

</div>

I agree.

```julia
sum_of(xs...) = sum(xs)

```

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [April 4, 2018, 11:06am UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/5 "2018-04-04T11:06:53Z")

</div>

Strange question, I’m not sure I understand. You want to define `g` so that it only accepts a `Vararg` with exactly `n` elements, and give a `MethodError` otherwise? If that is what you indeed need, I guess I would suggest looking into `@generated` functions. If you don’t “need” to throw a `MethodError` but rather just sum `n` arguments (if available), why don’t you just do the following?

```julia
function f(n::Int)
    g(xs...) = sum(xs[1:n])
   # rest of code
end

```

---

<div class="post-metadata">

**Author:** ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)\
**Post date:** [April 4, 2018, 11:33am UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/6 "2018-04-04T11:33:19Z")

</div>

“sum” is a simplified example. I’m really not looking to replace sum. The real problem is that I want to write a generic maximum likelihood heuristic optimizer for arbitrary distributions. JuMP requires arguments passed as scalars. The question is whether one can dynamically define Varargs.

---

<div class="post-metadata">

**Author:** ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)\
**Post date:** [April 4, 2018, 11:35am UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/7 "2018-04-04T11:35:03Z")

</div>

Yes. I’m trying to define the method to have as precise a signature as possible. Loosening the signature is probably what I’ll do for now. I’ll look into @generated functions. Thanks for suggesting it. I saw something about this for the first time the other day but am not yet familiar with it.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 4, 2018, 12:55pm UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/8 "2018-04-04T12:55:48Z")

</div>

> [@atteson](#):
>
> I’m trying to define the method to have as precise a signature as possible.

You don’t want the method signature to depend on a runtime argument like `n` since that would force it to be recompiled every time you call your function `f` with a new argument value. Recompilation is slow — except in special cases, you typically only want to recompile when the argument _types_ change, not the values.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [April 4, 2018, 3:51pm UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/9 "2018-04-04T15:51:16Z")

</div>

> [@atteson](#):
>
> Yes. I’m trying to define the method to have as precise a signature as possible.

Is the following precise enough?

```julia
julia> f(xs::Float64...) = print("type: ", typeof(xs), ", length: ", length(xs))
julia> f(1.0,2.4)
type: Tuple{Float64,Float64}, length: 2

```

or

```julia
julia> g(xs::T...) where T = print("type: ", typeof(xs), ", length: ", length(xs))
julia> g(1,2,3)
type: Tuple{Int64,Int64,Int64}, length: 3
```

---

<div class="post-metadata">

**Author:** ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)\
**Post date:** [April 4, 2018, 5:31pm UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/10 "2018-04-04T17:31:33Z")

</div>

f doesn’t get called often. In the real example, it will get called and then passes g into a function which will likely call it many thousands of time.

---

<div class="post-metadata">

**Author:** ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)\
**Post date:** [April 4, 2018, 5:47pm UTC](https://discourse.julialang.org/t/dynamic-vararg-count/10144/11 "2018-04-04T17:47:10Z")

</div>

Thanks. That’s close to what I’m looking for. I’m using that for now. It still doesn’t restrict subsequent callers from using the wrong number of Float64’s (which will ultimately fail but I’d rather catch it earlier). Anyway, thanks.
