# Type unstability with args

**URL:** https://discourse.julialang.org/t/type-unstability-with-args/123987
**Category:** New to Julia
**Created:** [December 19, 2024, 11:03am UTC](https://discourse.julialang.org/t/type-unstability-with-args/123987 "2024-12-19T11:03:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hbrochet](https://avatars.discourse-cdn.com/v4/letter/h/47e85d/32.png) [@hbrochet](https://discourse.julialang.org/u/hbrochet)
#### Post date: [December 19, 2024, 11:03am UTC](https://discourse.julialang.org/t/type-unstability-with-args/123987/1 "2024-12-19T11:03:55Z")

</div>

Hello,

I would like to make the following function type stable:

> function f(args…)  
> t = Tuple([arg] for arg in args)  
> return t  
> end
> 
> f(1,2.0,“s”)

a run from @code\_warntype gives me

> julia\> @code\_warntype f2(1,2.0,“s”)  
> MethodInstance for f2(::Int64, ::Float64, ::String)  
> from f2(args…) @ Main REPL[40]:1  
> Arguments  
> #self#::Core.Const(Main.f2)  
> args::Tuple{Int64, Float64, String}  
> Locals  
> #25::var"#25#26"  
> t::Tuple{Vararg{Vector}}  
> Body::Tuple{Vararg{Vector}}  
> 1 ─ %1 = Main.Tuple::Core.Const(Tuple)  
> │ %2 = Main.:(var"#25#26")::Core.Const(var"#25#26")  
> │ (#25 = %new(%2))  
> │ %4 = #25::Core.Const(var"#25#26"())  
> │ %5 = Base.Generator(%4, args)::Base.Generator{Tuple{Int64, Float64, String}, var"#25#26"}  
> │ (t = (%1)(%5))  
> │ %7 = t::Tuple{Vararg{Vector}}  
> └── return %7

The type of args is inferred correctly. I thought it would be enough to guess the correct type of t but somehow there is a Vararg which pops up.

Any help would be appreciated.

---

<div class="post-metadata">

### Author: ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)
#### Post date: [December 19, 2024, 11:42am UTC](https://discourse.julialang.org/t/type-unstability-with-args/123987/2 "2024-12-19T11:42:21Z")

</div>

The issue is that looping over the tuple results in the element type being inferred as `Any`. (that could possibly be changed, to be fair).  
So, instead of using a for loop, you can access the tuple elements in a different manner. For example, here, I access them by indexing:

```julia
function f(args...)
    ntuple(length(args)) do i
        [args[i]]
    end
end

```

This is type stable.

---

<div class="post-metadata">

### Author: ![hbrochet](https://avatars.discourse-cdn.com/v4/letter/h/47e85d/32.png) [@hbrochet](https://discourse.julialang.org/u/hbrochet)
#### Post date: [December 19, 2024, 11:52am UTC](https://discourse.julialang.org/t/type-unstability-with-args/123987/3 "2024-12-19T11:52:13Z")

</div>

Thank you very much it works !  
I was not aware of this method to create ntuple.

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [December 19, 2024, 1:17pm UTC](https://discourse.julialang.org/t/type-unstability-with-args/123987/4 "2024-12-19T13:17:24Z")

</div>

`map` should be type stable too

```julia
map(args) do x
    [x]
end

```
