composite types and fieldnames

154 views
Skip to first unread message

Tamas Papp

unread,
May 1, 2015, 9:57:22 AM5/1/15
to julia-dev
Hi,

I thought I would ask before reporting as a bug.

If isa(T, DataType) but !Base.isstructtype(T), is fieldnames(T) supposed
to return an empty vector or throw an error?

Currently (v"0.4.0-dev+4524") the first one happens:

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

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

But the error message in the code

function fieldnames(v)
t = typeof(v)
if !isa(t,DataType)
throw(ArgumentError("cannot call fieldnames() on a non-composite type"))
end
return fieldnames(t)
end

suggests that this is not what is supposed to happen. Would submit a PR,
but I need to wrap my head around variations of DataType and how to
interpret the fields. Would it make sense to extend the type hierarchy
with something like

CompositeType <: DataType
MutableType <: CompositeType
ImmutableType <: DataType
AbstractType <: DataType

?

Best,

Tamas

Jameson Nash

unread,
May 1, 2015, 11:45:42 AM5/1/15
to juli...@googlegroups.com
There used to be lots of different types of Type, but now everything is just an instance of a DataType. The properties of that datatype might vary (isbitstype, isstructtype, ismutable, isabstract, isleaftype, etc.), but it doesn't require a new type of Type to reflect those distinctions, a simple flag is enough.

Tamas Papp

unread,
May 2, 2015, 8:20:16 AM5/2/15
to juli...@googlegroups.com
Thanks for clarifying.

So is it intentional that fieldnames is defined for non-composite types,
returning an empty vector?

Best,

Tamas

Jameson Nash

unread,
May 2, 2015, 1:03:50 PM5/2/15
to juli...@googlegroups.com
Yes, it is reusing the common structure; a bitstype is a type with a non-zero sizeof but no fields. 

On Fri, May 1, 2015 at 9:57 AM Tamas Papp <tkp...@gmail.com> wrote:

Toivo Henningsson

unread,
May 3, 2015, 1:28:01 AM5/3/15
to juli...@googlegroups.com
Is that behavior actually useful somewhere. If I all for the last of field names I'd expect that those fields describe all the data stored in the type, so it would seem safer to throw an error if that is an option.
Reply all
Reply to author
Forward
0 new messages