# Why will we have to say fieldnames(typeof(v)) instead of fieldnames(v)?

**URL:** <https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129>\
**Category:** General Usage\
**Created:** [February 17, 2018, 3:58pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129 "2018-02-17T15:58:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 17, 2018, 3:58pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/1 "2018-02-17T15:58:44Z")

</div>

Sorry, I can’t understand these type of changes. On 0.7 we now get

```julia
Warning: `fieldnames(v)` is deprecated, use `fieldnames(typeof(v))` instead.

```

And when the deprecation stage is over one will get only a, +/- comprehensible, error message.  
The old behavior was much more user friendly. Why making life of users more difficult?

---

<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:** [February 17, 2018, 4:22pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/2 "2018-02-17T16:22:07Z")

</div>

Because the old one is very dangerous. The function had to guess if you want to know something about the type or the type of the type. Try `fieldnames(Vector)` and `fieldnames(Array)` on 0.6.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 17, 2018, 4:31pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/3 "2018-02-17T16:31:41Z")

</div>

Making life of users harder is also dangerous, like for another example the `findfirst` function.

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [February 17, 2018, 4:56pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/4 "2018-02-17T16:56:52Z")

</div>

Agree with the general statement. But in this case I think it’s _easier_ for a user. I remember playing around with `typeof` s, `supertype` s and `fieldnames` and was wondering why `fieldnames(v)` would work. Shouldn’t it be an instance of a type which has the fieldnames? With this change I think it’s clearer b/c there is no confusion between value and type.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 17, 2018, 5:05pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/5 "2018-02-17T17:05:50Z")

</div>

Sorry, I must be missing something (did not understand the `fieldnames(Vector)` example). When would the fildnames of a type and of an instance of that type be different?

---

<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:** [February 17, 2018, 5:07pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/6 "2018-02-17T17:07:13Z")

</div>

A type also has a type.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 17, 2018, 5:11pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/7 "2018-02-17T17:11:31Z")

</div>

And so that would be the case when one would have to use `fieldnames(typeof(type))`, which you’ll agree is a much rarer case than the `Joe normal user wanting to know the fields of a certain struct`

---

<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:** [February 17, 2018, 5:13pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/8 "2018-02-17T17:13:41Z")

</div>

Guessing what Joe-normal-user wants seems like shaky grounds to build a predictable system on. What if he is not around to ask.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 17, 2018, 5:16pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/9 "2018-02-17T17:16:40Z")

</div>

> [@kristoffer.carlsson](#):
>
> What if he is not around to ask.

But he is. This forum has many. Me for example.

---

<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:** [February 17, 2018, 6:27pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/10 "2018-02-17T18:27:38Z")

</div>

> [@joa-quim](#):
>
> Making life of users harder is also dangerous

No, absolutely not. There is no risk at all of giving the wrong answer silently.

> [@joa-quim](#):
>
> did not understand the fieldnames(Vector) example

Sorry, meant to say 0.5. not 0.6.

> [@joa-quim](#):
>
> And so that would be the case when one would have to use fieldnames(typeof(type)),

And doing something at all for those cases is terrible. So basically if the function is to do both things, it would have to raise an error for the ambiguous case, which means that for those ambiguous case which are actually useful, the function cannot possibly do anything. As a more concrete example, if you want to know what’s the field of a `Vector` type, you need to figure out a way to get from it to `Array` (And not the thing that binds to the `Array` name, but the `Array` datatype).

> [@joa-quim](#):
>
> But he is. This forum has many. Me for example.

So you think it is better to implement a function that send you a message on the forum whenever it encounters an ambiguous case?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 17, 2018, 6:37pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/11 "2018-02-17T18:37:55Z")

</div>

The issue here is also that we have different views on the importance of simplicity. If the two user situations are so important, create a new function `parentfieldnames(...)`

---

<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:** [February 17, 2018, 6:47pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/12 "2018-02-17T18:47:53Z")

</div>

> [@joa-quim](#):
>
> If the two user situations are so important, create a new function parentfieldnames(…)

I’m not sure what do you mean by two user situations and what is `parentfieldnames` supposed to do but if you want to have a function that treat everything as value instead of types, you can have your own function to do that. You can even implement your own heuristic to guess what you want to check in the repl. The reverse cannot be done.

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [February 17, 2018, 7:29pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/13 "2018-02-17T19:29:28Z")

</div>

> [@joa-quim](#):
>
> importance of simplicity

But isn’t the simplicity here only the fact, that one has to enter fewer character in the keyboard?

In `x = Dict("hi"=>"world")` the _variable_ `x` doesn’t have fieldnames AFAIU. Only its _type_, `typeof(x)` has. And `fieldnames(typeof(x))` shows the fields which can be found in the ‘mutable struct’ definition in base/dict.jl. To know the type of the Dict, a DataType, and the fieldnames thereof, it’s `fieldnames(typeof(typeof(x)))` and the definition can be found in base/boot.jl. This seems easy and consistent.

But `fieldname(x)` is confusing for me. If this gives the fieldnames of the `Dict` type, why doesn’t `fieldname(typeof(x))` give the fieldnames of the `DataType` type? Why should a DataType be treated differently than a (e.g.) Dict type?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 17, 2018, 7:49pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/14 "2018-02-17T19:49:52Z")

</div>

> [@swissr](#):
>
> But isn’t the simplicity here only the fact, that one has to enter fewer character in the keyboard?

Not at all my motivation here.

> [@swissr](#):
>
> In x = Dict(“hi”=\>“world”) the variable x doesn’t have fieldnames AFAIU. Only its type, typeof(x) has.

In my view of simplicity, if the variable has no fields, it would say so in output. Like it does in

```julia
julia> fieldnames(0)
0-element Array{Symbol,1}

```

otherwise if it has then list them. I’m just imagining the number of surprised users in future when they will get an error when doing `fieldnames(some_var_type)` and will have to go dig the manual why so.

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [February 17, 2018, 7:55pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/15 "2018-02-17T19:55:03Z")

</div>

> [@joa-quim](#):
>
> I’m just imagining the number of surprised users in future when they will get an error when doing fieldnames(some\_var\_type) and will have to go dig the manual why so.

On the plus side, the `error` could stay informative: if passed something that is not a type `fieldnames` could explain in the error what is now explained in the warning.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 17, 2018, 7:59pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/16 "2018-02-17T19:59:24Z")

</div>

> [@piever](#):
>
> On the plus side, the error could stay informative: if passed something that is not a type fieldnames could explain in the error what is now explained in the warning.

Than, if it can identify the situation, why not just do what it was asked in first place?

---

<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:** [February 17, 2018, 9:33pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/17 "2018-02-17T21:33:18Z")

</div>

> [@joa-quim](#):
>
> if the variable has no fields

A variable never has any field. A variable has a value. As I said, if you want to have a function that tell you what fields a value has, go ahead and do that yourself, just make sure that it is not the same function that tells you what field a type has.

> [@joa-quim](#):
>
> why not just do what it was asked in first place?

Because it it impossible in the ambiguous case!

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 17, 2018, 11:48pm UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/18 "2018-02-17T23:48:53Z")

</div>

> [@yuyichao](#):
>
> As I said, if you want to have a function that tell you what fields a value has, go ahead and do that yourself, just make sure that it is not the same function that tells you what field a type has.

It’s not about me. Honestly it’s my presumption (hopefuly wrong) that in name of such purist principles Julia will be sending away many users with unfriendly behaviors like this.

---

<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:** [February 18, 2018, 12:20am UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/19 "2018-02-18T00:20:23Z")

</div>

> [@joa-quim](#):
>
> sending away many users

That’s so much better than having a bad function that hurt actual users from time to time since it’s impossible to do the right guess all the time.

There’s also nothing unfriendly about this. FWIW, it’s the job of the help system and you are using the wrong function to begin with.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [February 18, 2018, 12:24am UTC](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129/20 "2018-02-18T00:24:23Z")

</div>

It’s trivial to define: `fieldnames_t(v) = fieldnames(typeof(v))`, which avoids the problems that Yu Yichao and others have brought up, while keeping what you want relatively short and sweet.

[Next page](https://discourse.julialang.org/t/why-will-we-have-to-say-fieldnames-typeof-v-instead-of-fieldnames-v/9129.md?page=2)
