tecosaur/About.jl

More informative string output

Closed this issue · 3 comments

Currently, about seems to be defined for strings, but isn't able to compute the memory layout.

julia> str = "Hello, I am writing some text."
"Hello, I am wrinting some text."

julia> about(str)
51-codeunit String (mutable), 51B referencing 59B (<: AbstractString <: Any)
ERROR: ArgumentError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer
Stacktrace:
  [1] _empty_reduce_error()
    @ Base ./reduce.jl:319
  [2] mapreduce_empty(f::Function, op::Function, T::Type)
    @ Base ./reduce.jl:321
[...]
 [10] maximum
    @ ./reducedim.jl:981 [inlined]
 [11] memorylayout(io::Base.TTY, value::String)
    @ About ~/.julia/packages/About/XPN7V/src/values.jl:60
 [12] about(io::Base.TTY, value::String)
    @ About ~/.julia/packages/About/XPN7V/src/values.jl:18
 [13] about(x::String)
    @ About ~/.julia/packages/About/XPN7V/src/About.jl:24
 [14] top-level scope
    @ REPL[13]:1

On About v0.1.0, julia 1.11.

Ah, the problem here is that fieldnames(String) is an empty tuple. I think the fix here is that the generic method should skip cases where the struct apparently has no fields (isstructtype(String) = true), and then cases where there is something else that would be sensible to do (like String) can implement dedicated memorylayout methods.

With recent changes, this no longer errors.

julia> about("Hello, I am wrinting some text.")
31-codeunit String (mutable), 31B referencing 39B (<: AbstractString <: Any)

I think we can do a little better though, stay tuned 🙂

I'd say this is good enough for now:

image