Conversation
|
I like this idea! I wonder if there's a slightly more general version—but I wouldn't want to worsen the ergonomics for this use-case. For example, I thought of dictionary types, which similarly suffer from their key–value relationships not being communicated to the pretty printer, but where the keys need not be keywords. A complication is that, for I also wondered about a generalization to more than two components to try to keep together, but that seemed like a point of diminishing usefulness. Maybe the answer is that the differences would be big enough that they shouldn't conflated with |
|
One possible answer (don’t know if it’s a good one or not) could be to keep |
When using `make-constructor-style-printer`, the current guidance for handling fields that are passed to the struct's constructor by name instead of by position is to use `unquoted-printing-string` to insert quoteless keywords into the printed representation. This has downsides when the struct is too long to print on a single line with the pretty printer. For example, printing `(foo #:a 1 #:b 2)` over multiple lines would print out thusly:
```
(foo
#:a
1
#:b
2)
```
This is unreadable. Far better is to keep each keyword and argument on the same line unless *that specific field* is the one that's long enough to force the line break. And in that instance, it would be better to indent the field value slightly to better communicate via indentation that it's associated with the keyword on the line above it. Supposing that the `#:a` field in the example above was the one that was excessively long, the above would be more readable like this:
```
(foo
#:a
1
#:b 2)
```
This commit adds a `keyword-prefixed-field` struct wrapping a keyword and an arbitrary value which, when printed, implements the above behavior for a single keyword field. By combining this with `make-constructor-style-printer`, we can achieve the desired pretty printing behavior.
500c65b to
ad38ea8
Compare
|
Should |
|
I don't think |
|
@LiberalArtist Regarding generalization, I think there's some options we could explore. But I'd like to handle them separately, in future PRs. Right now all I personally need is this specific API, and I think it's useful and ergonomic enough on its own that we'd still want it as-is even if we had a more general solution. |
Checklist
Description of change
When using
make-constructor-style-printer, the current guidance for handling fields that are passed to the struct's constructor by name instead of by position is to useunquoted-printing-stringto insert quoteless keywords into the printed representation. This has downsides when the struct is too long to print on a single line with the pretty printer. For example, printing(foo #:a 1 #:b 2)over multiple lines would print out thusly:This is unreadable. Far better is to keep each keyword and argument on the same line unless that specific field is the one that's long enough to force the line break. And in that instance, it would be better to indent the field value slightly to better communicate via indentation that it's associated with the keyword on the line above it. Supposing that the
#:afield in the example above was the one that was excessively long, the above would be more readable like this:This commit adds a
keyword-prefixed-fieldstruct wrapping a keyword and an arbitrary value which, when printed, implements the above behavior for a single keyword field. By combining this withmake-constructor-style-printer, we can achieve the desired pretty printing behavior.