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

Allow user to overload printing behavior #535

Open
MilesCranmer opened this issue Jan 17, 2024 · 2 comments · May be fixed by #537
Open

Allow user to overload printing behavior #535

MilesCranmer opened this issue Jan 17, 2024 · 2 comments · May be fixed by #537
Labels
enhancement New feature or request

Comments

@MilesCranmer
Copy link

MilesCranmer commented Jan 17, 2024

(x-ref https://discourse.julialang.org/t/proposed-alias-for-union-types/108205/142?u=milescranmer)

Julia’s type system is the best I’ve encountered in any language, but it can admittedly get a bit verbose at times. In particular things like optimizer options can make extensive use of type parameters and fill up multiple lines. Especially as some types stretch over the VSCode length limit in the Cthulhu labels and end up missing some information.

Therefore I was wondering if Cthulhu could permit the user to overload printing behavior for types in a Cthulhu printout, without necessarily overloading the printing behavior for all of Julia, like Float32 → F32, Tuple → Tu, etc, just to make the printouts less verbose.

For example:

import Cthulhu: type_string

## default defined in Cthulhu:
# type_string(t) = string(t)

# user aliases:
type_string(::Type{Float64}) = "F64"
type_string(::Type{Float32}) = "F32"

type_string(::Type{Missing}) = "?"

type_string(::Type{UInt8}) = "U8"

# Union{A,B} -> (A|B)
type_string(U::Union) = "(" * join(type_string.(union_to_tuple(U)), "|") * ")"
union_to_tuple(U::Union) = (union_to_tuple(U.a)..., union_to_tuple(U.b)...)
union_to_tuple(T::DataType) = (T,)

# Tuple{A,B} -> Tu{A,B}
type_string(::Type{T}) where {T<:Tuple} = "Tu{" * join(type_string.(T.parameters), ",") * "}"

# etc.

which you could have in your startup.jl. This would mean you get compact printouts like this:

julia> type_string(Tuple{Union{Float32,UInt8},Float64,Union{Float64,Missing},Missing})
"Tu{(F32|U8),F64,(?|F64),?}"

This is obviously a subjective choice to implement, but if it is simply something users can configure to their needs, it would be great!

This would be backwards compatible of course as it would simply expose an interface.

Cheers,
Miles

@Zentrik
Copy link
Collaborator

Zentrik commented Jan 17, 2024

If I'm remembering correctly this is the relevant line in the source code.

T_str = string(T)

This seems like an interesting idea and probably simple to implement.

@MilesCranmer
Copy link
Author

Thanks! I made a PR in #537.

Example:
Screenshot 2024-01-17 at 21 46 24

@simeonschaub simeonschaub added the enhancement New feature or request label Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants