Open
Description
proposal
add a std/prettyprints
module to allow pretty printing arbitrary expressions
design goal
As with std/jsonutils this should have:
- sane defaults
- provide enough options so you don't need to roll your own (eg custom depth etc)
- customizable via hooks for the cases where existing options aren't enough
- allows pretty-printing cyclic data structures
- accepts any expression (including ptr|pointer|etc)
- builtin
$
stays simple (I'd still argue$
works for ref|ptr|pointer for all targets (c,cpp,js,vm) + other bugfixes Nim#14043 should be merged in some form or another but that's orhtogonal to this RFC)
unlike dollars.nim, this is not a low-level module, and can have (at least optionally) heaver dependencies (eg tables.nim) for needed functionality and to simplify implementation
eg:
type Option = ref object
depth: int # how far to recurse
showDistinct: bool # when true, if no `$` overload exists for a distinct type, print via undistinct
fpFormat: string # customizes FP printing eg `"%.02f"`
# etc... that customizes prettyprinting
pretty[T](result: var string, a: T, options: Options = nil) = ...
template pretty[T](a: T, options: Options = nil): string = pretty(result, a, options)
# for convenience since so common
all overloads are outplace
form and there's a single inplace
form, just like in #191
- distinct can be shown via undistinct depending on
showDistinct
- we use a
Hash[ByteAddress]
to mark already visited addresses and in case input is cyclic
I've already implemented that in a private project with extensive options, so I know it's quite doable.
sample output
links
- originally proposed as a comment here: Every value should be printable with echo #203 (comment); see that link for previous discussions
- subsumes astalgo.debug
- see also
$
works for ref|ptr|pointer for all targets (c,cpp,js,vm) + other bugfixes Nim#14043 - Every value should be printable with echo #203
- it works even better with
getTypeId
, from new: typetraits.getTypeId Nim#13305, as explained in new: typetraits.getTypeId Nim#13305 (comment) - it works even better with scope based symbol resolution, refs scope based symbol resolution; fixes generic sandwich problem; fix #13747, #17965, #13970: declarations at non-module scope now work with generics Nim#18050 (to pickup a custom
$
in scope)
Metadata
Metadata
Assignees
Labels
No labels