# Use "end" as NamedTuple key or struct field

**URL:** https://discourse.julialang.org/t/use-end-as-namedtuple-key-or-struct-field/51862
**Category:** Internals & Design
**Created:** [December 15, 2020, 11:44am UTC](https://discourse.julialang.org/t/use-end-as-namedtuple-key-or-struct-field/51862 "2020-12-15T11:44:29Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![marlin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marlin/32/20291_2.png) [@marlin](https://discourse.julialang.org/u/marlin)
#### Post date: [December 15, 2020, 11:44am UTC](https://discourse.julialang.org/t/use-end-as-namedtuple-key-or-struct-field/51862/1 "2020-12-15T11:44:29Z")

</div>

I wanted to use “end” as a key of NamedTuple, however:

```julia
julia> (start = 1, end = 2)
ERROR: syntax: unexpected "end"

```

While I understand “end” is a reserved keyword, I guess there is no ambiguity if it is inside parentheses so we may parse it correctly.

My current workaround is:

```julia
julia> NamedTuple{Tuple([:start, :end])}([1,2]).end
2

```

which is a little bit ugly.

Another case is to use “end” as a struct field, the workaround would be even more ugly:

```julia
struct Range
           start
           end_
end
function Base.getproperty(x::Range, symbol::Symbol)
       if symbol === :end
           getfield(x, :end_)
       else
           getfield(x, symbol)
       end
end

```

I am not sure about the struct field case, but I feel using arbitrary key for a named tuple would be a valid usage. Any opinions?

---

<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: [December 15, 2020, 12:02pm UTC](https://discourse.julialang.org/t/use-end-as-namedtuple-key-or-struct-field/51862/2 "2020-12-15T12:02:03Z")

</div>

imo use of core tokens for other purposes is an anti-pattern  
[the source text is not just for you today, it is for you in six months and, perhaps for others who – at first reading, prefer disambiguity]

---

<div class="post-metadata">

### Author: ![marlin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marlin/32/20291_2.png) [@marlin](https://discourse.julialang.org/u/marlin)
#### Post date: [December 15, 2020, 12:16pm UTC](https://discourse.julialang.org/t/use-end-as-namedtuple-key-or-struct-field/51862/3 "2020-12-15T12:16:23Z")

</div>

Agree on the struct field case, but not named tuple.

Named tuple is used by people to represent Table rows (e.g. TypedTables.jl) and it should allow arbitrary key. As such `(start = 1, end = 2)` is much easier to read than `NamedTuple{Tuple([:start, :end])}([1,2])`

---

<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: [December 15, 2020, 12:31pm UTC](https://discourse.julialang.org/t/use-end-as-namedtuple-key-or-struct-field/51862/4 "2020-12-15T12:31:05Z")

</div>

You can use the constructor

```julia
julia> NamedTuple{(:start,:end)}((1, 2))
(start = 1, end = 2)

```

---

<div class="post-metadata">

### Author: ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)
#### Post date: [December 15, 2020, 6:47pm UTC](https://discourse.julialang.org/t/use-end-as-namedtuple-key-or-struct-field/51862/5 "2020-12-15T18:47:39Z")

</div>

I agree. Seems better, in this case particularly, to just use a different word. For example:

```julia
julia> (start = 1, stop = 2)

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [December 15, 2020, 7:38pm UTC](https://discourse.julialang.org/t/use-end-as-namedtuple-key-or-struct-field/51862/6 "2020-12-15T19:38:48Z")

</div>

```julia
julia> a = 1:3;

julia> dump(a)
UnitRange{Int64}
  start: Int64 1
  stop: Int64 3

```

which is exactly what julia base does 😉
