# Struct wrapping NamedTuple is not \`isbits\` (depending on how it is defined)

**URL:** <https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965>\
**Category:** General Usage\
**Tags:** memory-allocation, namedtuple\
**Created:** [December 12, 2021, 5:48am UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965 "2021-12-12T05:48:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [December 12, 2021, 5:48am UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/1 "2021-12-12T05:48:49Z")

</div>

I am wrapping NamedTuple to define some new/different behaviour without committing type piracy and ran into the following problem:

```nohighlight
struct Wrapper{SYMS, TT}
   x::NamedTuple{SYMS, TT}
end
randnt() = Wrapper((a = randn(), b = rand(), c = rand(1:5), d = randn(SVector{3, Float64})))
X = randnt()
@allocated randnt() # 64
isbits(X) # false
isbits(X.x) # true 

```

This is not good, I really need `Wrapper` to be non-allocating. Does this have anything to do with the fact that `Symbol` is not `isbits`?

Bizarrely, though, (for me anyhow…)

```nohighlight
struct Wrapper1{NT} 
   x::NT
end
randnt1() = Wrapper1((a = randn(), b = rand(), c = rand(1:5), d = randn(SVector{3, Float64})))
X1 = randnt1()
@allocated randnt1() # 0 
isbits(X1) # true
isbits(X1.x) # true

```

I cannot grasp the fundamental different between these two.

To explain why I prefer the first implementation `Wrapper`: I use quite a few generated functions which need the `SYMS` information. With a tiny bit of extra code this can of course be done for `Wrapper1` as well, so not too big a deal. (well-somewhat, I now have to fix a few 100 lines of code and not introduce bugs). But I am very curious and would like to understand what the difference here is?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 12, 2021, 9:21am UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/2 "2021-12-12T09:21:02Z")

</div>

Isn’t the named tuple still abstract because the length is not defined?

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [December 12, 2021, 2:51pm UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/3 "2021-12-12T14:51:24Z")

</div>

I don’t think so. The codes all seem type stable.

---

<div class="post-metadata">

**Author:** ![septc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/septc/32/2980_2.png) [@septc](https://discourse.julialang.org/u/septc)\
**Post date:** [December 12, 2021, 3:00pm UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/4 "2021-12-12T15:00:29Z")

</div>

On my computer (old mac), both the first and second codes give “0 true true” with Julia 1.6.1. So the behavior may depend on versions?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 12, 2021, 7:08pm UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/5 "2021-12-12T19:08:07Z")

</div>

Same here:

```julia
julia> VERSION, isbits(X), isbits(X.x)
(v"1.6.1", true, true)

```

```julia
julia> VERSION, isbits(X), isbits(X.x)
(v"1.7.0", false, true)

```

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [December 12, 2021, 7:20pm UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/6 "2021-12-12T19:20:26Z")

</div>

That would explain why I never noticed it until now 🙂

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 12, 2021, 11:00pm UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/7 "2021-12-12T23:00:11Z")

</div>

Uh oh. That might be a bug then, I’d recommend opening an issue if there’s not one already.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [December 12, 2021, 11:00pm UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/8 "2021-12-12T23:00:29Z")

</div>

Will do … [EDIT: done](https://github.com/JuliaLang/julia/issues/43411)

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 12, 2021, 11:10pm UTC](https://discourse.julialang.org/t/struct-wrapping-namedtuple-is-not-isbits-depending-on-how-it-is-defined/72965/9 "2021-12-12T23:10:38Z")

</div>

Yeah, definitely looks like a bug to me:

```julia
julia> nt(::Wrapper{S,T}) where {S,T} = NamedTuple{S,T}
nt (generic function with 1 method)

julia> nt(X)
NamedTuple{(:a, :b, :c, :d), Tuple{Float64, Float64, Int64, SVector{3, Float64}}}

julia> nt(X) === typeof(X.x)
true

julia> isbits(X) # false
false

julia> isbits(X.x) # true
true

```

The wrapper seems to be matching the `NamedTuple` exactly.
