# Get fieldnames and values of \`struct\` as NamedTuple

**URL:** <https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991>\
**Category:** General Usage\
**Created:** [February 11, 2018, 12:49pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991 "2018-02-11T12:49:26Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 21, 2018, 12:25pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/21 "2018-11-21T12:25:35Z")

</div>

Usually, if I have defined something like `Base.length` it was done as a matter of intercooperative necessity [something important would not be computable without the specialization of e.g. length to the given data type. That is my officialing.

N.B. Julia Base owns NamedTuples – I know why there is no definition of length there – I needed it in NamedTupleTools to play well with others.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [November 21, 2018, 12:52pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/22 "2018-11-21T12:52:46Z")

</div>

> N.B. Julia Base owns NamedTuples – I know why there is no definition of length there

Sure, that’s safe. We just have to make sure that other packages won’t do it, too. I would assume at least part of NamedTupleTools would be a candidate of inclusion into Base in future Julia versions anyhow, right?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 21, 2018, 1:02pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/23 "2018-11-21T13:02:54Z")

</div>

That is a matter for the Community.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 21, 2018, 1:05pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/24 "2018-11-21T13:05:22Z")

</div>

> [@oschulz](#):
>
> at least part of NamedTupleTools would be a candidate of inclusion into Base in future Julia versions anyhow, right?

I think the general tendency is to move things out of `Base`, so I would not assume this.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 21, 2018, 1:11pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/25 "2018-11-21T13:11:38Z")

</div>

That is type piracy and it typically means the package should not be used other than for interactive work and should not be used as a dependency since just loading the package can change behavior of existing code.

If certain Base methods are needed on Base types then having a discussion about adding them to Base is a good idea.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [November 21, 2018, 1:58pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/26 "2018-11-21T13:58:57Z")

</div>

> That is type piracy …

That was my worry here, yes.

> I think the general tendency is to move things out of `Base` , so I would not assume this.

I know. However, I think conversion between (simple) structs and tuples is something that would fit well into Base, and as @kristoffer.carlsson writes no other package outside of Base should define things like `convert(::Type{NamedTuple}, ...)`.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [November 21, 2018, 2:51pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/27 "2018-11-21T14:51:09Z")

</div>

FWIW

```julia
julia> x=(;a=2, b=3); @which length(x)
length(t::NamedTuple) in Base at namedtuple.jl:97

```

Doesn’t that work for you?

I don’t think that the `NamedTuple` constructor needs to be a pirate, since the user needs to spell it out anyway. It could be a different name, or a different function with the same name.

`Base.convert` is a problem though.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 21, 2018, 10:36pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/28 "2018-11-21T22:36:51Z")

</div>

> [@oschulz](#):
>
> I think conversion between (simple) structs and tuples is something that would fit well into Base

I don’t think these should exist as `convert` (I don’t think these should happen implicitly) but as a `make_struct(::Type{MyStruct}, ::NamedTuple)` or something (and at this point it isn’t type piracy of course).

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [November 21, 2018, 11:48pm UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/29 "2018-11-21T23:48:26Z")

</div>

> [@kristoffer.carlsson](#):
>
> but as a `make_struct(::Type{MyStruct}, ::NamedTuple)` or something (and at this point it isn’t type piracy of course).

Yes, I have something like that in the works at the moment, with functions `de_struct` and `re_struct`: [https://github.com/oschulz/ArraysOfStructs.jl/blob/dev/src/namedtuple\_utils.jl](https://github.com/oschulz/ArraysOfStructs.jl/blob/dev/src/namedtuple_utils.jl)

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [November 26, 2018, 12:44am UTC](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991/30 "2018-11-26T00:44:23Z")

</div>

NamedTupleTools (v0.5.0) is now piracy-free.

[Previous page](https://discourse.julialang.org/t/get-fieldnames-and-values-of-struct-as-namedtuple/8991.md?page=1)
