Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repr(::Vec) is broken #125

Open
dzhang314 opened this issue Sep 7, 2024 · 0 comments · May be fixed by #134
Open

repr(::Vec) is broken #125

dzhang314 opened this issue Sep 7, 2024 · 0 comments · May be fixed by #134

Comments

@dzhang314
Copy link

dzhang314 commented Sep 7, 2024

               _
   _       _ _(_)_     |  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, "))")
@KristofferC KristofferC linked a pull request Nov 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant