# \#ntuple

**URL:** https://discourse.julialang.org/tag/ntuple/993.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [StaticStrings.jl v0.3, move to JuliaStrings](https://discourse.julialang.org/t/staticstrings-jl-v0-3-move-to-juliastrings/139648)

<div class="topic-metadata">

**Author:** [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Replies:** 7\
**Last updated:** [September 25, 2026, 8:54am UTC](https://discourse.julialang.org/t/staticstrings-jl-v0-3-move-to-juliastrings/139648 "2026-09-25T08:54:42Z")

</div>

StaticStrings v0.3.0 StaticStrings.jl v0.3 was just released and has joined the JuliaStrings Github organization. Special thanks to @PatrickHaecker for major contributions to this release. StaticStrings.jl contains seve…

---

## [NTuple type vs conversion inconsistency](https://discourse.julialang.org/t/ntuple-type-vs-conversion-inconsistency/139005)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 8\
**Last updated:** [August 23, 2026, 12:47pm UTC](https://discourse.julialang.org/t/ntuple-type-vs-conversion-inconsistency/139005 "2026-08-23T12:47:12Z")

</div>

I find this a bit weird: julia\> x = (1, "a fish") (1, "a fish") julia\> x isa NTuple{2} false julia\> NTuple{2}(x) ≡ x true That is, T(x) ≡ x yet x is not a T.

---

## [Type instability with (fairly) simple Tuples](https://discourse.julialang.org/t/type-instability-with-fairly-simple-tuples/133304)

<div class="topic-metadata">

**Author:** [@johnomotani](https://discourse.julialang.org/u/johnomotani)\
**Replies:** 7\
**Last updated:** [October 20, 2025, 12:25pm UTC](https://discourse.julialang.org/t/type-instability-with-fairly-simple-tuples/133304 "2025-10-20T12:25:03Z")

</div>

Should I expect the following to be type-stable? I think the types are inferrable in principle, but maybe the Tuple-of-Pairs structure is just too ‘complicated’? julia\> using Cthulhu julia\> function foo(t::Pair{Symbol,…

---

## [How to sort NTuple](https://discourse.julialang.org/t/how-to-sort-ntuple/11535)

<div class="topic-metadata">

**Author:** [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Replies:** 5\
**Last updated:** [January 5, 2025, 12:57pm UTC](https://discourse.julialang.org/t/how-to-sort-ntuple/11535 "2025-01-05T12:57:53Z")

</div>

I have several integers input from a function like this function foo(a::Int...) end How do I sort a? I have to collect it first and sort it as a vector at the moment. Will it possible to sort it directly?

---

## [When to use Tuples instead of Vectors?](https://discourse.julialang.org/t/when-to-use-tuples-instead-of-vectors/119109)

<div class="topic-metadata">

**Author:** [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Replies:** 16\
**Last updated:** [September 6, 2024, 3:42pm UTC](https://discourse.julialang.org/t/when-to-use-tuples-instead-of-vectors/119109 "2024-09-06T15:42:49Z")

</div>

Hello, I’m developing QuantumToolbox.jl for simulating quantum systems in Julia. Everything is based on the QuantumObject constructor, which is currently defined as follows struct QuantumObject{MT\<:AbstractArray,ObjTyp…

---

## [Static array or tuple allocate at construction](https://discourse.julialang.org/t/static-array-or-tuple-allocate-at-construction/111447)

<div class="topic-metadata">

**Author:** [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)\
**Replies:** 2\
**Last updated:** [March 11, 2024, 9:11am UTC](https://discourse.julialang.org/t/static-array-or-tuple-allocate-at-construction/111447 "2024-03-11T09:11:28Z")

</div>

I have some code that allocates on the heap (and shouldn’t) while constructing a StaticArray. The hunt leads me to this MWE: using StaticArrays, BenchmarkTools @btime SVector{4,Float64}(2. \*i for i=1:4) # good, no…

---

## [Type intersection with \`NTuple{len,Any}\`](https://discourse.julialang.org/t/type-intersection-with-ntuple-len-any/109765)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 1\
**Last updated:** [February 6, 2024, 2:30pm UTC](https://discourse.julialang.org/t/type-intersection-with-ntuple-len-any/109765 "2024-02-06T14:30:53Z")

</div>

Problem: say I have a type T such that T \<: Tuple, and len, a nonnegative Int value. I want the intersection of T and NTuple{len,Any}. Is it possible to solve the above correctly and exactly? I have three candidate solu…

---

## [How to strip tuple type of the element types](https://discourse.julialang.org/t/how-to-strip-tuple-type-of-the-element-types/109664)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 6\
**Last updated:** [February 3, 2024, 9:09pm UTC](https://discourse.julialang.org/t/how-to-strip-tuple-type-of-the-element-types/109664 "2024-02-03T21:09:57Z")

</div>

I want a function taking a type T\<:Tuple type and returning S\<:Tuple type such that T\<:S and the element types are stripped from the input, but with preserved length information. Desired behavior examples: f(Tuple{}) =…

---

## [NTuple{N}(v) silently truncates when length(v) \> N](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 8\
**Last updated:** [January 18, 2024, 6:16pm UTC](https://discourse.julialang.org/t/ntuple-n-v-silently-truncates-when-length-v-n/108959 "2024-01-18T18:16:23Z")

</div>

MWE: julia\> NTuple{3}(zeros(4)) (0.0, 0.0, 0.0) I expected an error, am I missing something? (on Julia 1.10, master, 1.9, etc, this is not new)

---

## [Allocations when indexing (Abstract)Arrays with large dimension](https://discourse.julialang.org/t/allocations-when-indexing-abstract-arrays-with-large-dimension/103383)

<div class="topic-metadata">

**Author:** [@spionkaese](https://discourse.julialang.org/u/spionkaese)\
**Replies:** 6\
**Last updated:** [August 31, 2023, 9:49pm UTC](https://discourse.julialang.org/t/allocations-when-indexing-abstract-arrays-with-large-dimension/103383 "2023-08-31T21:49:30Z")

</div>

I’ve been working with tensors based on AbstractArrays (see https://gist.github.com/AlexanderNenninger/461f37315e45071a8c91b18d73901431 ) that map Base.getindex(A, i...) to a function call A.f(i). While the individual i…

---

## [Creating Sparse NTuple](https://discourse.julialang.org/t/creating-sparse-ntuple/103350)

<div class="topic-metadata">

**Author:** [@bmit](https://discourse.julialang.org/u/bmit)\
**Replies:** 4\
**Last updated:** [August 30, 2023, 10:06pm UTC](https://discourse.julialang.org/t/creating-sparse-ntuple/103350 "2023-08-30T22:06:51Z")

</div>

I have a problem where I want to make an NTuple of length N where I’m given indices and values for the non-zero components. The equivalent function for creating a Vector would look something like function foo(N, nzindi…

---

## [How to use @ntuple with N derived from parameters](https://discourse.julialang.org/t/how-to-use-ntuple-with-n-derived-from-parameters/100792)

<div class="topic-metadata">

**Author:** [@user664303](https://discourse.julialang.org/u/user664303)\
**Replies:** 4\
**Last updated:** [June 24, 2023, 9:20pm UTC](https://discourse.julialang.org/t/how-to-use-ntuple-with-n-derived-from-parameters/100792 "2023-06-24T21:20:56Z")

</div>

Below is a MWE of an error I get using @ntuple: using StaticArrays import Base.Cartesian: @ntuple function times2(v::SVector{N, T}) where {N, T} return SVector{N, T}(@ntuple N i-\>v\[i\]\*2) end a = randn(SVector{3, F…

---

## [Using ntuple() in generated function](https://discourse.julialang.org/t/using-ntuple-in-generated-function/100020)

<div class="topic-metadata">

**Author:** [@user664303](https://discourse.julialang.org/u/user664303)\
**Replies:** 5\
**Last updated:** [June 8, 2023, 12:48am UTC](https://discourse.julialang.org/t/using-ntuple-in-generated-function/100020 "2023-06-08T00:48:47Z")

</div>

I have found that using ntuple in generated functions produces better code (faster, and with fewer allocations), than constructing the tuple expression manually. Presumably the code that ntuple generates is more inferabl…

---

## [Using large NTuples makes Julia hang](https://discourse.julialang.org/t/using-large-ntuples-makes-julia-hang/78900)

<div class="topic-metadata">

**Author:** [@Maurizio\_Tomasi](https://discourse.julialang.org/u/Maurizio_Tomasi)\
**Replies:** 23\
**Last updated:** [February 10, 2023, 10:40am UTC](https://discourse.julialang.org/t/using-large-ntuples-makes-julia-hang/78900 "2023-02-10T10:40:10Z")

</div>

Julia 1.7.2 on Linux x86\_64 seems to hang when instantiating structures containing large NTuple types, as the following code shows: struct Foo id:: UInt64 tag::NTuple{32, UInt8} start\_comment::NTuple{4096, U…

---

## [Vararg (sub)type parameter undocumented dispatch weirdness](https://discourse.julialang.org/t/vararg-sub-type-parameter-undocumented-dispatch-weirdness/87110)

<div class="topic-metadata">

**Author:** [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Replies:** 7\
**Last updated:** [January 29, 2023, 12:02am UTC](https://discourse.julialang.org/t/vararg-sub-type-parameter-undocumented-dispatch-weirdness/87110 "2023-01-29T00:02:59Z")

</div>

a1(::Integer...) = nothing a2(::I...) where {I \<: Integer} = nothing a3(::Vararg{Integer}) = nothing a4(::Vararg{\<:Integer}) = nothing a5(::Vararg{I}) where {I \<: Integer} = nothing b3(::NTuple{n, Integer}) where {n} = n…

---

## [Field names and values of a struct defined as NTuple](https://discourse.julialang.org/t/field-names-and-values-of-a-struct-defined-as-ntuple/91284)

<div class="topic-metadata">

**Author:** [@mmv](https://discourse.julialang.org/u/mmv)\
**Replies:** 4\
**Last updated:** [December 6, 2022, 11:58pm UTC](https://discourse.julialang.org/t/field-names-and-values-of-a-struct-defined-as-ntuple/91284 "2022-12-06T23:58:42Z")

</div>

Hi! Let struct ex1 name::String age::Int end I understand that (1) the statements above define a new type of record containing a string and a number (2) an instance of that type can be created as a = ex1("Jo…

---

## [Fill function for a tuple](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400)

<div class="topic-metadata">

**Author:** [@james3](https://discourse.julialang.org/u/james3)\
**Replies:** 6\
**Last updated:** [August 8, 2022, 2:03pm UTC](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400 "2022-08-08T14:03:58Z")

</div>

I need to create a tuple with the value r repeated N times. Creating an array would be easy, as I could just do fill(r, N). How can I do this, but creating a tuple instead?

---

## [Puzzled by (abstract) NTuple conversion](https://discourse.julialang.org/t/puzzled-by-abstract-ntuple-conversion/8286)

<div class="topic-metadata">

**Author:** [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Replies:** 0\
**Last updated:** [January 11, 2018, 10:25am UTC](https://discourse.julialang.org/t/puzzled-by-abstract-ntuple-conversion/8286 "2018-01-11T10:25:50Z")

</div>

Is this expected? julia\> convert(NTuple{2,T} where T\<:AbstractFloat, (1, Float16(2.0))) (1.0, Float16(2.0)) julia\> ans isa NTuple{2,T} where T\<:AbstractFloat false I was under the impression that convert offered a gua…

---

## [\[ANN\] NTupleKmers.jl](https://discourse.julialang.org/t/ann-ntuplekmers-jl/40474)

<div class="topic-metadata">

**Author:** [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Replies:** 0\
**Last updated:** [May 30, 2020, 4:09pm UTC](https://discourse.julialang.org/t/ann-ntuplekmers-jl/40474 "2020-05-30T16:09:20Z")

</div>

Hi Everyone, Following my earlier murmerings on Kmers and NTuples on Slack and here: Ntuple aggressive specialisation and boxed values I decided to create an experimental package repo on BioJulia that can house my NTu…

---

## [Correct type in function for Dict of NTuple](https://discourse.julialang.org/t/correct-type-in-function-for-dict-of-ntuple/83474)

<div class="topic-metadata">

**Author:** [@christmas\_jones](https://discourse.julialang.org/u/christmas_jones)\
**Replies:** 2\
**Last updated:** [June 28, 2022, 6:12pm UTC](https://discourse.julialang.org/t/correct-type-in-function-for-dict-of-ntuple/83474 "2022-06-28T18:12:55Z")

</div>

I need a function which takes a Dictionary, where keys are Integer Tuples, and values are Float64 types. I do not know in advance the length of the Tuples, so I would be tempted to use the following type in the function…

---

## [Why do large NTuples Allocate?](https://discourse.julialang.org/t/why-do-large-ntuples-allocate/73145)

<div class="topic-metadata">

**Author:** [@jg-854](https://discourse.julialang.org/u/jg-854)\
**Replies:** 6\
**Last updated:** [December 15, 2021, 3:13pm UTC](https://discourse.julialang.org/t/why-do-large-ntuples-allocate/73145 "2021-12-15T15:13:35Z")

</div>

I was surprised to find that generating Tuples via generators causes allocations for large generators… Using BenchmarkTools @btime NTuple{5,Float64}(1.0 for i=1:5) 1.500 ns (0 allocations: 0 bytes) @btime NTuple{50…

---

## [Why NTuple{2, Ref{Int}} \<: NTuple get false?](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559)

<div class="topic-metadata">

**Author:** [@zwtechnb](https://discourse.julialang.org/u/zwtechnb)\
**Replies:** 7\
**Last updated:** [October 11, 2021, 7:23pm UTC](https://discourse.julialang.org/t/why-ntuple-2-ref-int-ntuple-get-false/69559 "2021-10-11T19:23:07Z")

</div>

Code: julia\> NTuple{2, Int} \<: NTuple true julia\> NTuple{2, Ref{Int}} \<: NTuple false julia\> (NTuple{2, T} where T) \<: NTuple true

---

## [Specifying structs with variably nested NTuple fields](https://discourse.julialang.org/t/specifying-structs-with-variably-nested-ntuple-fields/65817)

<div class="topic-metadata">

**Author:** [@tchr](https://discourse.julialang.org/u/tchr)\
**Replies:** 4\
**Last updated:** [August 5, 2021, 5:02pm UTC](https://discourse.julialang.org/t/specifying-structs-with-variably-nested-ntuple-fields/65817 "2021-08-05T17:02:12Z")

</div>

I suspect what I want isn’t possible but wanted to ask here to be certain (or to learn precisely why it isn’t possible). The setup for the question is as follows. Suppose I have a function: function nested\_ntuple(::Val…

---

## [Why is the NTuple{N} constructor not type stable when constructing a Tuple from a Vector?](https://discourse.julialang.org/t/why-is-the-ntuple-n-constructor-not-type-stable-when-constructing-a-tuple-from-a-vector/54185)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 1\
**Last updated:** [January 29, 2021, 2:28pm UTC](https://discourse.julialang.org/t/why-is-the-ntuple-n-constructor-not-type-stable-when-constructing-a-tuple-from-a-vector/54185 "2021-01-29T14:28:47Z")

</div>

julia\> @code\_warntype NTuple{3}(\[1,2,3\]) Variables #self#::Type{Tuple{T, T, T} where T} itr::Vector{Int64} Body::Tuple{Int64, Int64, Int64, Vararg{Any}} 1 ─ %1 = Base.\_totuple($(Expr(:static\_parameter, 1)), itr)::Tu…

---

## [How to make a particular NTuple based Type concrete](https://discourse.julialang.org/t/how-to-make-a-particular-ntuple-based-type-concrete/50846)

<div class="topic-metadata">

**Author:** [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Replies:** 3\
**Last updated:** [November 27, 2020, 6:27am UTC](https://discourse.julialang.org/t/how-to-make-a-particular-ntuple-based-type-concrete/50846 "2020-11-27T06:27:47Z")

</div>

I have a struct representing the remainders of an integer with respect to several moduli. All of the moduli fit into Int64 except for 2^64, so every modulus is either an Int64 or Type{Int64}. struct LooseGoose{N} …

---

## [Cryptic error message in constructing an NTuple](https://discourse.julialang.org/t/cryptic-error-message-in-constructing-an-ntuple/42149)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 4\
**Last updated:** [June 27, 2020, 11:16am UTC](https://discourse.julialang.org/t/cryptic-error-message-in-constructing-an-ntuple/42149 "2020-06-27T11:16:41Z")

</div>

I’m on Julia 1.4.2. This works julia\> NTuple{1}(3) (3,) This doesn’t, and doesn’t say why julia\> NTuple(3) ERROR: UndefVarError: E not defined Stacktrace: \[1\] \_totuple(::Type{Tuple{Vararg{T,N}} where T where N}, ::…

---

## [Ntuple aggressive specialisation and boxed values](https://discourse.julialang.org/t/ntuple-aggressive-specialisation-and-boxed-values/40450)

<div class="topic-metadata">

**Author:** [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Replies:** 4\
**Last updated:** [May 30, 2020, 5:28pm UTC](https://discourse.julialang.org/t/ntuple-aggressive-specialisation-and-boxed-values/40450 "2020-05-30T17:28:48Z")

</div>

Hi, Further to my questions on the General slack today I decided to do an experiment, pitting an NTuple based kmer type, against the existing BioSequences Mer types which are based on primitive types. Valentin advised …

---

## [Best way to convert a NTuple to a Set](https://discourse.julialang.org/t/best-way-to-convert-a-ntuple-to-a-set/27532)

<div class="topic-metadata">

**Author:** [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Replies:** 2\
**Last updated:** [August 14, 2019, 8:48pm UTC](https://discourse.julialang.org/t/best-way-to-convert-a-ntuple-to-a-set/27532 "2019-08-14T20:48:51Z")

</div>

I did: julia\> ntup = (1, 2, 3, 4, 5, 6), NTuple{6, Int} julia\> Set(ntup) Set(Any\[(1, 2, 3, 4, 5, 6), NTuple{6,Int64}\]) But I want to get a set of Int64. How can I achieve that?

---

## [Mutate NTuple at position](https://discourse.julialang.org/t/mutate-ntuple-at-position/17682)

<div class="topic-metadata">

**Author:** [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Replies:** 7\
**Last updated:** [November 19, 2018, 6:49am UTC](https://discourse.julialang.org/t/mutate-ntuple-at-position/17682 "2018-11-19T06:49:21Z")

</div>

I want to switch out an NTuple’s element, like this: julia\> function switch\_k(tup::NTuple{N,T}, elem::T, pos::Integer) where {N,T} #\_assume(0\<pos\<=N) return ntuple(i-\> (pos!==i) ? tup\[i\] : elem ,N) …

---

## [Why does \`NTuple{N, T}\`'s parameter order is not \`{T, N}\`?](https://discourse.julialang.org/t/why-does-ntuple-n-t-s-parameter-order-is-not-t-n/14935)

<div class="topic-metadata">

**Author:** [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Replies:** 6\
**Last updated:** [September 14, 2018, 8:42am UTC](https://discourse.julialang.org/t/why-does-ntuple-n-t-s-parameter-order-is-not-t-n/14935 "2018-09-14T08:42:35Z")

</div>

Consider AbstractArray{T,N} or Vararg{T,N}, whose orders are both {T, N}. So why do NTuple has a different order?

[Next page](https://discourse.julialang.org/tag/ntuple/993.md?match_all_tags=true&page=1&tags%5B%5D=ntuple)
