# NamedTuple with invalid variable names

**URL:** <https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [October 11, 2018, 10:09am UTC](https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167 "2018-10-11T10:09:04Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [October 11, 2018, 10:09am UTC](https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167/1 "2018-10-11T10:09:04Z")

</div>

1. Are `NamedTuple`s with keys which are not valid variable names allowed? eg

2. Is there an easier syntax for them than the above?

I am asking because I am working on a package that would return column data with an iterator that yields NamedTuples, and it would be awkward not to handle weird column names.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [October 11, 2018, 10:35am UTC](https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167/2 "2018-10-11T10:35:24Z")

</div>

> [@Tamas\_Papp](#):
>
> NamedTuple{(Symbol(“1”), )}(19)

doesn’t work that well:

```julia
julia-1.0> t = NamedTuple{(Symbol("1"), )}(19)
(1 = 19,)

julia-1.0> t.1
ERROR: syntax: extra token "0.1" after end of expression

```

---

<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:** [October 11, 2018, 10:45am UTC](https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167/3 "2018-10-11T10:45:50Z")

</div>

It is still accessible by `t[Symbol("1")]`.

Just to clarify, the question is not about whether this is _convenient_, as it isn’t. I am asking whether it is part of the design, or just works accidentally.

---

<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:** [October 11, 2018, 1:05pm UTC](https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167/4 "2018-10-11T13:05:27Z")

</div>

I think it is totally fine.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [October 12, 2018, 9:02am UTC](https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167/5 "2018-10-12T09:02:08Z")

</div>

FYI a semicolon is useful for this

```julia
julia> (; :function => 1, Symbol("1") => 2)
(function = 1, 1 = 2)

```

---

<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:** [October 12, 2018, 9:33am UTC](https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167/6 "2018-10-12T09:33:07Z")

</div>

Null is forbidden, though:

```julia
julia> Symbol("a\0b")
ERROR: ArgumentError: Symbol name may not contain \0

```

And at least the REPL makes assumptions about symbols being valid encoded strings.

```julia
julia> s=rand(UInt8, 20) |> String |>Symbol;

julia> s
Error showing value of type Symbol:
ERROR: Base.InvalidCharError{Char}('\x83')

```

So I’d be mildly cautious before using Symbol for binary blobs.

---

<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:** [October 12, 2018, 9:36am UTC](https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167/7 "2018-10-12T09:36:50Z")

</div>

> [@foobar\_lv2](#):
>
> So I’d be mildly cautious before using Symbol for binary blobs.

Thanks, but they are not binary blobs but columns names (eg the first row in a CSV file). I think that in this context it is fair to say that if someone wants to put a `\0` in a variable name, they should reflect on the wisdom of this choice.

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [October 14, 2018, 7:49pm UTC](https://discourse.julialang.org/t/namedtuple-with-invalid-variable-names/16167/8 "2018-10-14T19:49:45Z")

</div>

I agree with the assessment that this is allowable, just inconvenient. As long as there are no embedded nul characters it should be fine.
