# Static fieldnames

**URL:** <https://discourse.julialang.org/t/static-fieldnames/5619>\
**Category:** General Usage\
**Created:** [August 28, 2017, 8:10pm UTC](https://discourse.julialang.org/t/static-fieldnames/5619 "2017-08-28T20:10:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [August 28, 2017, 8:10pm UTC](https://discourse.julialang.org/t/static-fieldnames/5619/1 "2017-08-28T20:10:54Z")

</div>

Imagine I have a

```julia
struct A
    a::Int 
    b::String 
end

```

I want to stringify it like this:  
`string(A.a, A.b)`

However, I want to do this WITHOUT prior knowledge of the fieldnames of A. This is quite difficult to do in a type-stable way. It would be much easier if for any type one could generate a tuple `Val{:a}(), Val{:b}()` where a and b are fieldnames. Then the fieldnames would be known to the compiler. Through the magic of tuples, it would in fact be quite easy to do type stably:

```julia
inner_type(::Val{T}) where T = T

function stringify(object)
    value_fields = get_value_fields(typeof(object))
    string(map(value_fields) do value_field
        getindex(object, inner_type(value_field))
    end...)
end

```

Is this possible somehow?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 28, 2017, 8:33pm UTC](https://discourse.julialang.org/t/static-fieldnames/5619/2 "2017-08-28T20:33:51Z")

</div>

```julia
julia> @generated f(obj) = :(string($((:(getfield(obj, $i)) for i in 1:fieldcount(obj))...)))
f (generic function with 1 method)

julia> f(1im)
"01"

julia> f((1, 2, 2.4))
"122.4"

julia> @code_warntype f(1im)
Variables:
  #self# <optimized out>
  obj::Complex{Int64}

Body:
  begin
      #= REPL[4]:1 =#
      # meta: location REPL[4] @generated body
      return $(Expr(:invoke, MethodInstance for #print_to_string#245(::Void, ::Function, ::Int64, ::Vararg{Int64,N} where N), :(Base.#print_to_string#245), :($(QuoteNode(nothing))), :($(QuoteNode(Base.print_to_string))), :((Main.getfield)(obj, 1)::Int64), :((Main.getfield)(obj, 2)::Int64)))::String
      # meta: pop location
  end::String

julia> @code_warntype f((1, 2, "", 3))
Variables:
  #self# <optimized out>
  obj::Tuple{Int64,Int64,String,Int64}

Body:
  begin
      #= REPL[4]:1 =#
      # meta: location REPL[4] @generated body
      return $(Expr(:invoke, MethodInstance for #print_to_string#245(::Void, ::Function, ::Int64, ::Vararg{Any,N} where N), :(Base.#print_to_string#245), :($(QuoteNode(nothing))), :($(QuoteNode(Base.print_to_string))), :((Main.getfield)(obj, 1)::Int64), :((Main.getfield)(obj, 2)::Int64), :((Main.getfield)(obj, 3)::String), :((Main.getfield)(obj, 4)::Int64)))::String
      # meta: pop location
  end::String

```

?

---

<div class="post-metadata">

**Author:** ![jrevels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jrevels/32/10393_2.png) [@jrevels](https://discourse.julialang.org/u/jrevels)\
**Post date:** [August 28, 2017, 8:33pm UTC](https://discourse.julialang.org/t/static-fieldnames/5619/3 "2017-08-28T20:33:52Z")

</div>

```julia
julia> @generated function stringify(x::T) where {T}
           return :(string($([:(getfield(x, $name)) for name in fieldnames(T)]...)))
       end
stringify (generic function with 1 method)

julia> struct Foo
           a
           b
       end

julia> stringify(Foo("hello ", "bramtayl"))
"hello bramtayl"

```

EDIT: @yuyichao was quick on the draw and beat me to it 😛

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [August 28, 2017, 8:41pm UTC](https://discourse.julialang.org/t/static-fieldnames/5619/4 "2017-08-28T20:41:37Z")

</div>

Isn’t there some sort of taboo on generated functions?

---

<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:** [August 28, 2017, 10:21pm UTC](https://discourse.julialang.org/t/static-fieldnames/5619/5 "2017-08-28T22:21:40Z")

</div>

Taboo? They are quite easy to misuse but for this case it seems like a perfect tool.
