# How to create structs with fields that are reserved Julia keywords

**URL:** <https://discourse.julialang.org/t/how-to-create-structs-with-fields-that-are-reserved-julia-keywords/65784>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [August 4, 2021, 5:04am UTC](https://discourse.julialang.org/t/how-to-create-structs-with-fields-that-are-reserved-julia-keywords/65784 "2021-08-04T05:04:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![orso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orso/32/27818_2.png) [@orso](https://discourse.julialang.org/u/orso)\
**Post date:** [August 4, 2021, 5:04am UTC](https://discourse.julialang.org/t/how-to-create-structs-with-fields-that-are-reserved-julia-keywords/65784/1 "2021-08-04T05:04:53Z")

</div>

I do not seem to be able to create structs that have reserved Julia keywords as fields  
[Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#Keywords)

For example:

```julia
struct test
   local::Float64
end

```

will throw a `syntax: malformed expression` error

Because I want to match an existing naming convention I do not have the freedom to choose whatever struct field names I’d like.

Is there any to create structs with fields that are reserved Julia keywords? I think it should be possible, after all something like the following syntax should not be confusing:

```julia
tmp = test()
println(tmp.local)

```

---

<div class="post-metadata">

**Author:** ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)\
**Post date:** [August 4, 2021, 5:19am UTC](https://discourse.julialang.org/t/how-to-create-structs-with-fields-that-are-reserved-julia-keywords/65784/2 "2021-08-04T05:19:03Z")

</div>

see `var"name"`  
[https://docs.julialang.org/en/v1/base/base/#var"name"](https://docs.julialang.org/en/v1/base/base/#var%22name%22)

```julia
julia> struct test
          var"local"::Float64
       end

julia> tmp = test(1.2)
test(1.2)

julia> tmp.local
1.2

julia> tmp.var"local"
1.2

```

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 4, 2021, 5:23am UTC](https://discourse.julialang.org/t/how-to-create-structs-with-fields-that-are-reserved-julia-keywords/65784/3 "2021-08-04T05:23:28Z")

</div>

See [the explanation of `var"name"`](https://docs.julialang.org/en/v1/base/base/#var%22name%22)

Input

```julia
struct Foo
    var"local"::Float64
end
@show foo = Foo(1.2)
@show foo.local;

```

Output

```julia
foo = Foo(1.2) = Foo(1.2)
foo.local = 1.2

```

Input (how to look at the parser result of the code above.)

```julia
:(struct Foo
    var"local"::Float64
end) |> Base.remove_linenums! |> print

```

Output

```julia
struct Foo
    local::Float64
end

```

Edit: Wow! It’s a coincidence that the numbers in the two examples are both 1.2!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 4, 2021, 6:23am UTC](https://discourse.julialang.org/t/how-to-create-structs-with-fields-that-are-reserved-julia-keywords/65784/4 "2021-08-04T06:23:03Z")

</div>

Here’s an alternative approach, which is more verbose, but perhaps more familiar-looking:

```julia
struct Test
    _local::Float64
end
function Base.getproperty(t::Test, sym::Symbol)
    if sym == :local
        return t._local
    else
        return getfield(t, sym)
    end
end

jl> t = Test(1.2)
Test(1.2)

jl> t.local
1.2

```

Mandating particular field names is questionable practice though, and the names shouldn’t really be part of an interface. Why is this done? Accessor functions are normally preferred.

---

<div class="post-metadata">

**Author:** ![orso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orso/32/27818_2.png) [@orso](https://discourse.julialang.org/u/orso)\
**Post date:** [August 4, 2021, 4:34pm UTC](https://discourse.julialang.org/t/how-to-create-structs-with-fields-that-are-reserved-julia-keywords/65784/5 "2021-08-04T16:34:16Z")

</div>

Excellent! Thank you for pointing me to the `var` keyword. That works beautifully for my application.

I’ll also think about developing accessor functions, but it is handy/easier to keep the same field names that were used in the original data structure that I am replicating in Julia.
