`isdefined` documentation

Hi

I am very confused about the documentation of the isdefined function.

What exactly is the index mentioned in the documentation? Or the symbol (I assume a field in a struct)? The example with a = 1//2 is not very useful as it stands, given that it does not fully explain what is going on.

MA

The fields have an order. You can pick :num which is the first one or :den which is the second one.

julia> a = 1//2
1//2

julia> fieldnames(typeof(a))
(:num, :den)

julia> fieldnames(typeof(a))[1]
:num

julia> fieldnames(typeof(a))[2]
:den
1 Like

The symbol is explained in the docs, isn’t it? It’s the field name as you suggest. I think the index is just the index of the fields, but I agree the docs could be clearer on this, feel free to make a PR:

julia> dump(a)
Rational{Int64}
  num: Int64 1
  den: Int64 2

julia> isdefined(a, :num)
true

julia> isdefined(a, 1)
true

julia> isdefined(a, 2)
true

julia> isdefined(a, 3)
false

Thank you all.

Let’s say that your responses should be included in the documentation.

But, apart from that… I have

julia> qd = 42

Pardon my obtuseness, but how exactly am I supposed to check that qd is defined? And no, I do not want to use the @isdefined macro. Must I use the version with the module as first argument?

Thanks

julia> isdefined(Main, :qd)
true

julia> isdefined(@__MODULE__, :qd)
true

julia> isdefined(@__MODULE__, :qe)
false
3 Likes

Why not?

julia> @isdefined qd
true

Looks like the easy way to get what you want.

Anyone is more than welcome to submit a patch to clarify the documentation. The isdefined docstring is in basedocs.jl, and you can click the edit button to modify it and propose a change.

I know it is easy and it does what is usually needed. But the point was to grok isdefined.

1 Like

Thanks @stevengj . That is a useful suggestion.

Duh! :face_holding_back_tears:

Things that happen when your memory is short (and wired a liiiiitle differently).

Thanks.