Skip to content

repr(::Vec) is broken #125

Open
Open
@dzhang314

Description

@dzhang314
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.10.5 (2024-08-27)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using SIMD

julia> println(repr(Vec(1.0, 2.0, 3.0, 4.0)))
<4 x Float64>[1.0, 2.0, 3.0, 4.0]

julia> <4 x Float64>[1.0, 2.0, 3.0, 4.0]
ERROR: ParseError:
# Error @ REPL[3]:1:1
<4 x Float64>[1.0, 2.0, 3.0, 4.0]
╙ ── not a unary operator
Stacktrace:
 [1] top-level scope
   @ none:1

In general, the output of repr(x) is supposed to be an executable Julia expression that evaluates to x. I just fixed the same issue in MultiFloats.jl and realized it also exists upstream in SIMD.jl.

You can see the approach I took to fix this problem in commit c996294. Basically, instead of overloading Base.show(::IO, ::MultiFloat) directly, I separately overload Base.show(::IO, ::MIME"text/plain", ::MultiFloat) and Base.print(::IO, ::MultiFloat) to print the pretty representation of a MultiFloat, which lets the default Base.show implementation print the ugly-but-executable representation. This still prints the pretty representation in functions like string and println and the Julia/IJulia REPL.

In fact, feel free to use my code for showing a Vec:

        show(io, Vec{N,T})
        write(io, "((")
        for i = 1:N
            if i > 1
                write(io, ", ")
            end
            show(io, vec[i])
        end
        write(io, "))")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions