-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Description
I was kind of hoping we'd define a decent
--trimfallback forshow_default, e.g. w/@generatedto iterate struct fields type-stably.
Originally posted by @topolarity in #59598 (review)
show_default is a "default implementation" in Base - it's an automatic implementation of printing based purely on structural / type information.
The current version in Base._show_default iterates the fields dynamically (simplified):
for i in 1:nf
show(recur_io, getfield(x, i))
# ...
endwhich is inherently type-unstable (and is additionally marked @nospecialize, so getfield(x, i) is inferred ::Any for any argument anywhere). This is fine for typical Julia execution, where printing is not especially performance-sensitive, and instead it's beneficial to avoid generating specialized code for all structs (hence the @nospecialize)
For --trim compatibility, we need an @generated equivalent that behaves similarly to #[derive(Display)] in Rust and inspects the type structurally and generates a type-stable implementation of show_fallback for the type.