Skip to content

Commit

Permalink
names -> fieldnames rename in Base, deprecate names()
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolewski committed Feb 26, 2015
1 parent 31acbca commit cbc538d
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 42 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
6 changes: 3 additions & 3 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function completes_global(x, name)
end

function filtered_mod_names(ffunc::Function, mod::Module, name::AbstractString, all::Bool=false, imported::Bool=false)
ssyms = names(mod, all, imported)
ssyms = fieldnames(mod, all, imported)
filter!(ffunc, ssyms)
syms = UTF8String[string(s) for s in ssyms]
filter!(x->completes_global(x, name), syms)
Expand Down 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
8 changes: 8 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,11 @@ end
# 8898
@deprecate precision(x::DateTime) eps(x)
@deprecate precision(x::Date) eps(x)

@deprecate names fieldnames

@deprecate names(m::Module, all::Bool, imported::Bool) fieldnames(m, all, imported)
@deprecate names(m::Module, all::Bool) fieldnames(m, all)
@deprecate names(m::Module) fieldnames(m)
@deprecate names(t::DataType) fieldnames(t)
@deprecate names(v) fieldnames(v)
4 changes: 2 additions & 2 deletions base/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ moduleusings(mod) = ccall(:jl_module_usings, Any, (Any,), mod)
filtervalid(names) = filter(x->!ismatch(r"#", x), map(string, names))

accessible(mod::Module) =
[names(mod, true, true);
map(names, moduleusings(mod))...;
[fieldnames(mod, true, true);
map(fieldnames, moduleusings(mod))...;
builtins] |> unique |> filtervalid

completions(name) = fuzzysort(name, accessible(current_module()))
Expand Down
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/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ end

function methodswith(t::Type, m::Module, showparents::Bool=false)
meths = Method[]
for nm in names(m)
for nm in fieldnames(m)
if isdefined(m, nm)
f = eval(m, nm)
if isa(f, Function)
Expand All @@ -315,7 +315,7 @@ function methodswith(t::Type, showparents::Bool=false)
meths = Method[]
mainmod = current_module()
# find modules in Main
for nm in names(mainmod)
for nm in fieldnames(mainmod)
if isdefined(mainmod,nm)
mod = eval(mainmod, nm)
if isa(mod, Module)
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
23 changes: 11 additions & 12 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,21 @@ 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)
fieldnames(m::Module, all::Bool, imported::Bool) = ccall(:jl_module_fieldnames, Array{Symbol,1}, (Any,Int32,Int32), m, all, imported)
fieldnames(m::Module, all::Bool) = fieldnames(m, all, false)
fieldnames(m::Module) = fieldnames(m, false, false)
fieldnames(t::DataType) = collect(t.names)

function names(v)
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

isconst(s::Symbol) =
Expand Down Expand Up @@ -68,7 +67,7 @@ end
# subtypes
function _subtypes(m::Module, x::DataType, sts=Set(), visited=Set())
push!(visited, m)
for s in names(m,true)
for s in fieldnames(m,true)
if isdefined(m,s)
t = eval(m,s)
if isa(t, DataType) && t.name.name == s && super(t).name == x.name
Expand Down
23 changes: 12 additions & 11 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ macro show(exs...)
end

function show(io::IO, tn::TypeName)
if (tn.module == Core && tn.name in names(Core)) ||
(tn.module == Base && tn.name in names(Base)) ||
if (tn.module == Core && tn.name in fieldnames(Core)) ||
(tn.module == Base && tn.name in fieldnames(Base)) ||
tn.module == Main
print(io, tn.name)
else
Expand Down Expand Up @@ -738,7 +738,7 @@ function xdump(fn::Function, io::IO, x, n::Int, indent)
if isa(T, DataType) && length(T.names) > 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 +787,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 +817,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 Expand Up @@ -1112,7 +1113,7 @@ function show_nd(io::IO, a::AbstractArray, limit, print_matrix, label_slices)
end

function whos(m::Module, pattern::Regex)
for v in sort(names(m))
for v in sort(fieldnames(m))
s = string(v)
if isdefined(m,v) && ismatch(pattern, s)
println(rpad(s, 30), summary(eval(m,v)))
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ 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]
offset = fieldoffsets(C_Sparse)[9] # field offset of C_Sparse.stype
unsafe_store!(convert(Ptr{Cint}, A.p), i, div(offset, 4) + 1)
A
end
Expand Down
1 change: 1 addition & 0 deletions deps/Rmath
Submodule Rmath added at 226598
2 changes: 1 addition & 1 deletion src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ DLLEXPORT jl_value_t *jl_module_usings(jl_module_t *m)
return (jl_value_t*)a;
}

DLLEXPORT jl_value_t *jl_module_names(jl_module_t *m, int all, int imported)
DLLEXPORT jl_value_t *jl_module_fieldnames(jl_module_t *m, int all, int imported)
{
jl_array_t *a = jl_alloc_array_1d(jl_array_symbol_type, 0);
JL_GC_PUSH1(&a);
Expand Down

0 comments on commit cbc538d

Please sign in to comment.