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
Can we extend Base.@kwdef
to override default show()
to print via keyword argument construction?
#37518
Comments
Or maybe via a parameter, like |
A much cleaner way is to simply have a function that does the printing using keyword argument syntax and the user can simply use it with a single line of code. |
I think Parameters.jl defines a julia> using Parameters
julia> @with_kw struct Statistics
seen_count::Int = 0
recomputed_count::Int = 0
invalidated_count::Int = 0
end
Statistics
julia> stats = Statistics(seen_count=3, recomputed_count=2)
Statistics
seen_count: Int64 3
recomputed_count: Int64 2
invalidated_count: Int64 0 Maybe other packages do too :) |
People use
Base.@kwdef
for structs where they want users to set the fields by name. Can we extend this macro to also provide a definition forBase.show()
that prints the struct with all of its args by name, something like this?:EDIT: oh, duh, I guess this isn't possible because if the user then wants to provide their own
Base.show
, there would be a double-definition error. :/Is there any clever way around that?
Or could we maybe have something like
@kwdef_and_show
? Or another macro@kwshow
that could be composed with this macro? :)The text was updated successfully, but these errors were encountered: