@@ -883,99 +883,115 @@ function show(io::IO, tv::TypeVar)
883
883
end
884
884
end
885
885
886
- # dump & xdump - structured tree representation like R's str()
887
- # - dump is for the user-facing structure
888
- # - xdump is for the internal structure
889
- #
890
- # x is the object
891
- # n is the depth of traversal in nested types (5 is the default)
892
- # indent is a character string of spaces that is incremented at
893
- # each descent.
894
- #
895
- # Package writers may overload dump for other nested types like lists
896
- # or DataFrames. If overloaded, check the nesting level (n), and if
897
- # n > 0, dump each component. Limit to the first 10 entries. When
898
- # dumping components, decrement n, and add two spaces to indent.
899
- #
900
- # Package writers should not overload xdump.
886
+ function dump (io:: IO , x:: SimpleVector , n:: Int , indent)
887
+ if isempty (x)
888
+ print (io, " empty SimpleVector" )
889
+ return
890
+ end
891
+ print (io, " SimpleVector" )
892
+ if n > 0
893
+ for i = 1 : length (x)
894
+ println (io)
895
+ print (io, indent, " " , i, " : " )
896
+ if isassigned (x,i)
897
+ dump (io, x[i], n - 1 , string (indent, " " ))
898
+ else
899
+ print (io, undef_ref_str)
900
+ end
901
+ end
902
+ end
903
+ end
901
904
902
- function xdump (fn :: Function , io:: IO , x, n:: Int , indent)
905
+ function dump ( io:: IO , x:: ANY , n:: Int , indent)
903
906
T = typeof (x)
904
- print (io, T, " " )
905
- if isa (T, DataType) && nfields (T) > 0
906
- println (io)
907
+ if isa (x, Function)
908
+ print (io, x, " (function of type " , T, " )" )
909
+ else
910
+ print (io, T)
911
+ end
912
+ if nfields (T) > 0
907
913
if n > 0
908
- for field in fieldnames (T)
909
- if field != symbol (" " ) # prevents segfault if symbol is blank
910
- print (io, indent, " " , field, " : " )
911
- if isdefined (x,field)
912
- fn (io, getfield (x, field), n - 1 , string (indent, " " ))
913
- else
914
- println (io, undef_ref_str)
915
- end
914
+ for field in (isa (x,Tuple) ? (1 : length (x)) : fieldnames (T))
915
+ println (io)
916
+ print (io, indent, " " , field, " : " )
917
+ if isdefined (x,field)
918
+ dump (io, getfield (x, field), n - 1 , string (indent, " " ))
919
+ else
920
+ print (io, undef_ref_str)
916
921
end
917
922
end
918
923
end
919
924
else
920
- println (io , x)
925
+ ! isa (x,Function) && print (io, " " , x)
921
926
end
927
+ nothing
922
928
end
923
- function xdump (fn:: Function , io:: IO , x:: Module , n:: Int , indent)
924
- print (io, Module, " " )
925
- println (io, x)
926
- end
927
- function xdump_elts (fn:: Function , io:: IO , x:: Array{Any} , n:: Int , indent, i0, i1)
929
+
930
+ dump (io:: IO , x:: Module , n:: Int , indent) = print (io, " Module " , x)
931
+
932
+ function dump_elts (io:: IO , x:: Array , n:: Int , indent, i0, i1)
928
933
for i in i0: i1
929
934
print (io, indent, " " , i, " : " )
930
935
if ! isdefined (x,i)
931
- println (io, undef_ref_str)
936
+ print (io, undef_ref_str)
932
937
else
933
- fn (io, x[i], n - 1 , string (indent, " " ))
938
+ dump (io, x[i], n - 1 , string (indent, " " ))
934
939
end
940
+ i < i1 && println (io)
935
941
end
936
942
end
937
- function xdump (fn:: Function , io:: IO , x:: Array{Any} , n:: Int , indent)
938
- println (io, " Array($(eltype (x)) ,$(size (x)) )" )
939
- if n > 0
940
- xdump_elts (fn, io, x, n, indent, 1 , (length (x) <= 10 ? length (x) : 5 ))
941
- if length (x) > 10
942
- println (io, indent, " ..." )
943
- xdump_elts (fn, io, x, n, indent, length (x)- 4 , length (x))
943
+
944
+ function dump (io:: IO , x:: Array , n:: Int , indent)
945
+ print (io, " Array($(eltype (x)) ,$(size (x)) )" )
946
+ if eltype (x) <: Number
947
+ print (io, " " )
948
+ show (io, x)
949
+ else
950
+ if n > 0 && ! isempty (x)
951
+ println (io)
952
+ if limit_output (io)
953
+ dump_elts (io, x, n, indent, 1 , (length (x) <= 10 ? length (x) : 5 ))
954
+ if length (x) > 10
955
+ println (io)
956
+ println (io, indent, " ..." )
957
+ dump_elts (io, x, n, indent, length (x)- 4 , length (x))
958
+ end
959
+ else
960
+ dump_elts (io, x, n, indent, 1 , length (x))
961
+ end
944
962
end
945
963
end
946
964
end
947
- xdump (fn:: Function , io:: IO , x:: Symbol , n:: Int , indent) = println (io, typeof (x), " " , x)
948
- xdump (fn:: Function , io:: IO , x:: Function , n:: Int , indent) = println (io, x)
949
- xdump (fn:: Function , io:: IO , x:: Array , n:: Int , indent) =
950
- (print (io, " Array($(eltype (x)) ,$(size (x)) ) " );
951
- show (io, x); println (io))
965
+ dump (io:: IO , x:: Symbol , n:: Int , indent) = print (io, typeof (x), " " , x)
952
966
953
967
# Types
954
- xdump (fn:: Function , io:: IO , x:: Union , n:: Int , indent) = println (io, x)
955
- function xdump (fn:: Function , io:: IO , x:: DataType , n:: Int , indent)
956
- println (io, x, " ::" , typeof (x), " " , " <: " , supertype (x))
957
- fields = fieldnames (x)
958
- if n > 0
959
- for idx in 1 : min (10 , length (fields))
960
- if fields[idx] != symbol (" " ) # prevents segfault if symbol is blank
968
+ dump (io:: IO , x:: Union , n:: Int , indent) = print (io, x)
969
+
970
+ function dump (io:: IO , x:: DataType , n:: Int , indent)
971
+ print (io, x)
972
+ if x != = Any
973
+ print (io, " <: " , supertype (x))
974
+ end
975
+ if ! (x <: Tuple )
976
+ fields = fieldnames (x)
977
+ if n > 0
978
+ for idx in 1 : length (fields)
979
+ println (io)
961
980
print (io, indent, " " , fields[idx], " ::" )
962
- if isa (x. types[idx], DataType)
963
- xdump (fn, io, fieldtype (x,idx), n - 1 , string (indent, " " ))
964
- else
965
- println (io, fieldtype (x,idx))
966
- end
981
+ # if isa(x.types[idx], DataType)
982
+ # xdump(fn, io, fieldtype(x,idx), n - 1, string(indent, " "))
983
+ # else
984
+ print (io, fieldtype (x,idx))
985
+ # end
967
986
end
968
987
end
969
- if length (fields) > 10
970
- println (io, indent, " ..." )
971
- end
972
988
end
973
989
end
974
990
975
991
# dumptype is for displaying abstract type hierarchies like Jameson
976
992
# Nash's wiki page: https://github.com/JuliaLang/julia/wiki/Types-Hierarchy
977
993
978
- function dumptype (io:: IO , x, n:: Int , indent)
994
+ function dumptype (io:: IO , x:: ANY , n:: Int , indent)
979
995
# based on Jameson Nash's examples/typetree.jl
980
996
println (io, x)
981
997
if n == 0 # too deeply nested
@@ -1016,50 +1032,12 @@ end
1016
1032
1017
1033
# For abstract types, use _dumptype only if it's a form that will be called
1018
1034
# interactively.
1019
- xdump (fn:: Function , io:: IO , x:: DataType ) = x. abstract ? dumptype (io, x, 5 , " " ) : xdump (fn, io, x, 5 , " " )
1020
- xdump (fn:: Function , io:: IO , x:: DataType , n:: Int ) = x. abstract ? dumptype (io, x, n, " " ) : xdump (fn, io, x, n, " " )
1021
-
1022
- # defaults:
1023
- xdump (fn:: Function , io:: IO , x) = xdump (xdump, io, x, 5 , " " ) # default is 5 levels
1024
- xdump (fn:: Function , io:: IO , x, n:: Int ) = xdump (xdump, io, x, n, " " )
1025
- xdump (fn:: Function , io:: IO , args... ) = throw (ArgumentError (" invalid arguments to xdump" ))
1026
- xdump (fn:: Function , args... ) = xdump (fn, STDOUT:: IO , args... )
1027
- xdump (io:: IO , args... ) = xdump (xdump, io, args... )
1028
- xdump (args... ) = xdump (xdump, IOContext (STDOUT:: IO , :limit_output => true ), args... )
1029
- xdump (arg:: IO ) = xdump (xdump, STDOUT:: IO , arg)
1030
-
1031
- # Here are methods specifically for dump:
1032
- dump (io:: IO , x, n:: Int ) = dump (io, x, n, " " )
1033
- dump (io:: IO , x) = dump (io, x, 5 , " " ) # default is 5 levels
1034
- dump (io:: IO , x:: AbstractString , n:: Int , indent) =
1035
- (print (io, typeof (x), " " );
1036
- show (io, x); println (io))
1037
- dump (io:: IO , x, n:: Int , indent) = xdump (dump, io, x, n, indent)
1038
- dump (io:: IO , args... ) = throw (ArgumentError (" invalid arguments to dump" ))
1039
- dump (arg:: IO ) = xdump (dump, STDOUT:: IO , arg)
1040
- dump (args... ) = dump (IOContext (STDOUT:: IO , :limit_output => true ), args... )
1041
-
1042
- function dump (io:: IO , x:: Dict , n:: Int , indent)
1043
- println (io, typeof (x), " len " , length (x))
1044
- if n > 0
1045
- i = 1
1046
- for (k,v) in x
1047
- print (io, indent, " " , k, " : " )
1048
- dump (io, v, n - 1 , string (indent, " " ))
1049
- if i > 10
1050
- println (io, indent, " ..." )
1051
- break
1052
- end
1053
- i += 1
1054
- end
1055
- end
1056
- end
1035
+ dflt_io () = IOContext (STDOUT:: IO , :limit_output => true )
1036
+ dump (io:: IO , x:: DataType ; maxdepth= 8 ) = (x. abstract ? dumptype : dump)(io, x, maxdepth, " " )
1037
+ dump (x:: DataType ; maxdepth= 8 ) = (x. abstract ? dumptype : dump)(dflt_io (), x, maxdepth, " " )
1057
1038
1058
- # More generic representation for common types:
1059
- dump (io:: IO , x:: DataType , n:: Int , indent) = println (io, x. name)
1060
- dump (io:: IO , x:: DataType , n:: Int ) = dump (io, x, n, " " )
1061
- dump (io:: IO , x:: DataType ) = dump (io, x, 5 , " " )
1062
- dump (io:: IO , x:: TypeVar , n:: Int , indent) = println (io, x. name)
1039
+ dump (io:: IO , arg; maxdepth= 8 ) = dump (io, arg, maxdepth, " " )
1040
+ dump (arg; maxdepth= 8 ) = dump (dflt_io (), arg, maxdepth, " " )
1063
1041
1064
1042
1065
1043
"""
0 commit comments