Skip to content

Commit

Permalink
names -> fieldnames rename for composite types in Base
Browse files Browse the repository at this point in the history
- deprecate names()
- abstract getting number of fields with `nfields`
  • Loading branch information
jakebolewski committed Feb 26, 2015
1 parent 93af265 commit dda4d73
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 50 deletions.
2 changes: 1 addition & 1 deletion base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ macro enum(T,syms...)
Base.typemin{E<:$(esc(typename))}(x::Type{E}) = E($lo)
Base.typemax{E<:$(esc(typename))}(x::Type{E}) = E($hi)
Base.length{E<:$(esc(typename))}(x::Type{E}) = $(length(vals))
Base.names{E<:$(esc(typename))}(x::Type{E}) = [$(map(x->Meta.quot(x[1]), vals)...)]
Base.next{E<:$(esc(typename))}(x::Type{E},s) = (E($values[s]),s+1)
Base.done{E<:$(esc(typename))}(x::Type{E},s) = s > $(length(values))
Base.names{E<:$(esc(typename))}(x::Type{E}) = [$(map(x->Meta.quot(x[1]), vals)...)]
function Base.print{E<:$(esc(typename))}(io::IO,x::E)
for (sym, i) in $vals
if i == x.val
Expand Down
4 changes: 2 additions & 2 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function complete_symbol(sym, ffunc)
end
else
# We're now looking for a type
fields = t.names
fields = fieldnames(t)
found = false
for i in 1:length(fields)
s == fields[i] || continue
Expand Down Expand Up @@ -79,7 +79,7 @@ function complete_symbol(sym, ffunc)
end
else
# Looking for a member of a type
fields = t.names
fields = fieldnames(t)
for field in fields
s = string(field)
if startswith(s, name)
Expand Down
7 changes: 4 additions & 3 deletions base/deepcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ function deepcopy_internal(x, stackdict::ObjectIdDict)
end

function _deepcopy_t(x, T::DataType, stackdict::ObjectIdDict)
isbits(T) | isempty(T.names) && return x
nf = nfields(T)
(isbits(T) || nf == 0) && return x
if T.mutable
y = ccall(:jl_new_struct_uninit, Any, (Any,), T)
stackdict[x] = y
for i in 1:length(T.names)
for i in 1:nf
if isdefined(x,i)
y.(i) = deepcopy_internal(x.(i), stackdict)
end
end
else
fields = Any[deepcopy_internal(x.(i), stackdict) for i in 1:length(T.names)]
fields = Any[deepcopy_internal(x.(i), stackdict) for i in 1:nf]
y = ccall(:jl_new_structv, Any, (Any, Ptr{Void}, UInt32),
T, pointer(fields), length(fields))
end
Expand Down
3 changes: 3 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,6 @@ end
# 8898
@deprecate precision(x::DateTime) eps(x)
@deprecate precision(x::Date) eps(x)

@deprecate names(t::DataType) fieldnames(t)
@deprecate names(v) fieldnames(v)
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ export
# types
convert,
fieldoffsets,
fieldnames,
isleaftype,
oftype,
promote,
Expand Down
5 changes: 3 additions & 2 deletions base/help.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ function help(io::IO, fname::AbstractString, obj=0)
println(io)
end
end
if length(obj.names) > 0
println(io, " fields : ", obj.names)
fields = fieldnames(obj)
if !isempty(fields)
println(io, " fields : ", fields)
end
elseif isgeneric(obj)
writemime(io, "text/plain", obj); println()
Expand Down
4 changes: 2 additions & 2 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ const getfield_tfunc = function (A, s0, name)
end
end
end
for i=1:length(s.names)
for i=1:nfields(s)
if is(s.names[i],fld)
R = s.types[i]
if s.parameters === ()
Expand All @@ -360,7 +360,7 @@ const getfield_tfunc = function (A, s0, name)
return Bottom
end
i::Int = A[2]
if i < 1 || i > length(s.names)
if i < 1 || i > nfields(s)
return Bottom
end
return s.types[i]
Expand Down
2 changes: 1 addition & 1 deletion base/markdown/Julia/interp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ toexpr(xs::Vector{Any}) = Expr(:cell1d, map(toexpr, xs)...)

function deftoexpr(T)
@eval function toexpr(md::$T)
Expr(:call, typeof(md), $(map(x->:(toexpr(md.$x)), names(T))...))
Expr(:call, typeof(md), $(map(x->:(toexpr(md.$x)), fieldnames(T))...))
end
end

Expand Down
4 changes: 2 additions & 2 deletions base/multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ type WorkerConfig
environ::Nullable{Dict}

function WorkerConfig()
wc=new()
for n in names(WorkerConfig)
wc = new()
for n in fieldnames(WorkerConfig)
T = eltype(fieldtype(WorkerConfig, n))
setfield!(wc, n, Nullable{T}())
end
Expand Down
8 changes: 4 additions & 4 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ let implemented = IntSet()
# Create the types
indextype = symbol("CartesianIndex_$N")
if !in(N,implemented)
fieldnames = [symbol("I_$i") for i = 1:N]
fields = [Expr(:(::), fieldnames[i], :Int) for i = 1:N]
fnames = [symbol("I_$i") for i = 1:N]
fields = [Expr(:(::), fnames[i], :Int) for i = 1:N]
extype = Expr(:type, false, Expr(:(<:), indextype, Expr(:curly, :CartesianIndex, N)), Expr(:block, fields...))
eval(extype)
argsleft = [Expr(:(::), fieldnames[i], :Real) for i = 1:N]
argsright = [Expr(:call,:to_index,fieldnames[i]) for i=1:N]
argsleft = [Expr(:(::), fnames[i], :Real) for i = 1:N]
argsright = [Expr(:call,:to_index,fnames[i]) for i=1:N]
exconstructor = Expr(:(=),Expr(:call,:(Base.call),:(::Type{CartesianIndex{$N}}),argsleft...),Expr(:call,indextype,argsright...))
eval(exconstructor)
push!(implemented,N)
Expand Down
30 changes: 20 additions & 10 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ current_module() = ccall(:jl_get_current_module, Any, ())::Module

function fullname(m::Module)
if m === Main
()
return ()
elseif module_parent(m) === m
# not Main, but is its own parent, means a prior Main module
n = ()
Expand All @@ -20,22 +20,32 @@ function fullname(m::Module)
end
return n
else
tuple(fullname(module_parent(m))..., module_name(m))
return tuple(fullname(module_parent(m))..., module_name(m))
end
end

names(m::Module, all::Bool, imported::Bool) = ccall(:jl_module_names, Array{Symbol,1}, (Any,Int32,Int32), m, all, imported)
names(m::Module, all::Bool) = names(m, all, false)
names(m::Module) = names(m, false, false)
names(t::DataType) = collect(t.names)

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

fieldname(t::DataType, i::Integer) = t.names[i]

nfields(t::DataType) = length(t.names)
function nfields(v)
t = typeof(v)
if !isa(DataType)
throw(ArgumentError("cannot call nfields() on a non-composite type"))
end
return nfields(t)
end

isconst(s::Symbol) =
Expand All @@ -49,7 +59,7 @@ object_id(x::ANY) = ccall(:jl_object_id, UInt, (Any,), x)

# type predicates
isimmutable(x::ANY) = (isa(x,Tuple) || !typeof(x).mutable)
isstructtype(t::DataType) = t.names!=() || (t.size==0 && !t.abstract)
isstructtype(t::DataType) = nfields(t) != 0 || (t.size==0 && !t.abstract)
isstructtype(x) = false
isbits(t::DataType) = !t.mutable & t.pointerfree & isleaftype(t)
isbits(t::Type) = false
Expand All @@ -60,7 +70,7 @@ typeintersect(a::ANY,b::ANY) = ccall(:jl_type_intersection, Any, (Any,Any), a, b
typeseq(a::ANY,b::ANY) = a<:b && b<:a

function fieldoffsets(x::DataType)
offsets = Array(Int, length(x.names))
offsets = Array(Int, nfields(x))
ccall(:jl_field_offsets, Void, (Any, Ptr{Int}), x, offsets)
offsets
end
Expand Down
11 changes: 6 additions & 5 deletions base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,12 @@ function serialize(s, x)
return write_as_tag(s, x)
end
t = typeof(x)
nf = nfields(t)
serialize_type(s, t)
if length(t.names)==0 && t.size>0
if nf == 0 && t.size > 0
write(s, x)
else
for i in 1:length(t.names)
for i in 1:nf
if isdefined(x, i)
serialize(s, getfield(x, i))
else
Expand Down Expand Up @@ -527,11 +528,11 @@ end

# default DataType deserializer
function deserialize(s, t::DataType)
if length(t.names)==0 && t.size>0
nf = nfields(t)
if nf == 0 && t.size > 0
# bits type
return read(s, t)
end
nf = length(t.names)
if nf == 0
return ccall(:jl_new_struct, Any, (Any,Any...), t)
elseif isbits(t)
Expand All @@ -552,7 +553,7 @@ function deserialize(s, t::DataType)
end
else
x = ccall(:jl_new_struct_uninit, Any, (Any,), t)
for i in 1:length(t.names)
for i in 1:nf
tag = int32(read(s, UInt8))
if tag==0 || !is(deser_tag[tag], UndefRefTag)
ccall(:jl_set_nth_field, Void, (Any, Csize_t, Any), x, i-1, handle_deserialize(s, tag))
Expand Down
30 changes: 16 additions & 14 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function show(io::IO, x::ANY)
t = typeof(x)::DataType
show(io, t)
print(io, '(')
if t.names !== () || t.size==0
nf = nfields(t)
if nf != 0 || t.size==0
recorded = false
oid = object_id(x)
shown_set = get(task_local_storage(), :SHOWNSET, nothing)
Expand All @@ -22,15 +23,15 @@ function show(io::IO, x::ANY)
push!(shown_set, oid)
recorded = true

n = length(t.names)
for i=1:n
f = t.names[i]
nf = nfields(t)
for i=1:nf
f = fieldname(t, i)
if !isdefined(x, f)
print(io, undef_ref_str)
else
show(io, x.(f))
end
if i < n
if i < nf
print(io, ',')
end
end
Expand Down Expand Up @@ -735,10 +736,10 @@ end
function xdump(fn::Function, io::IO, x, n::Int, indent)
T = typeof(x)
print(io, T, " ")
if isa(T, DataType) && length(T.names) > 0
if isa(T, DataType) && nfields(T) > 0
println(io)
if n > 0
for field in T.names
for field in fieldnames(T)
if field != symbol("") # prevents segfault if symbol is blank
print(io, indent, " ", field, ": ")
if isdefined(x,field)
Expand Down Expand Up @@ -787,18 +788,19 @@ xdump(fn::Function, io::IO, x::Array, n::Int, indent) =
xdump(fn::Function, io::IO, x::UnionType, n::Int, indent) = println(io, x)
function xdump(fn::Function, io::IO, x::DataType, n::Int, indent)
println(io, x, "::", typeof(x), " ", " <: ", super(x))
fields = fieldnames(x)
if n > 0
for idx in 1:min(10,length(x.names))
if x.names[idx] != symbol("") # prevents segfault if symbol is blank
print(io, indent, " ", x.names[idx], "::")
for idx in 1:min(10, length(fields))
if fields[idx] != symbol("") # prevents segfault if symbol is blank
print(io, indent, " ", fields[idx], "::")
if isa(x.types[idx], DataType)
xdump(fn, io, x.types[idx], n - 1, string(indent, " "))
xdump(fn, io, fieldtype(x,idx), n - 1, string(indent, " "))
else
println(io, x.types[idx])
println(io, fieldtype(x,idx))
end
end
end
if length(x.names) > 10
if length(fields) > 10
println(io, indent, " ...")
end
end
Expand All @@ -816,7 +818,7 @@ function dumptype(io::IO, x, n::Int, indent)
typargs(t) = split(string(t), "{")[1]
# todo: include current module?
for m in (Core, Base)
for s in names(m)
for s in fieldnames(m)
if isdefined(m,s)
t = eval(m,s)
if isa(t, TypeConstructor)
Expand Down
12 changes: 8 additions & 4 deletions base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -795,10 +795,14 @@ sparse(D::Dense) = sparse(Sparse(D))

# Calculate the offset into the stype field of the cholmod_sparse_struct and
# change the value
function change_stype!(A::Sparse, i::Integer)
offset = fieldoffsets(C_Sparse)[names(C_Sparse) .== :stype][1]
unsafe_store!(convert(Ptr{Cint}, A.p), i, div(offset, 4) + 1)
A
let offidx=findfirst(fieldnames(C_Sparse) .== :stype)

global change_stype!
function change_stype!(A::Sparse, i::Integer)
offset = fieldoffsets(C_Sparse)[offidx]
unsafe_store!(convert(Ptr{Cint}, A.p), i, div(offset, 4) + 1)
return A
end
end

free!(A::Dense) = free_dense!(A.p)
Expand Down

0 comments on commit dda4d73

Please sign in to comment.