dbuenzli / fmt

OCaml Format pretty-printer combinators
http://erratique.ch/software/fmt
ISC License
71 stars 26 forks source link

Add Fmt.nl #28

Closed rizo closed 7 years ago

rizo commented 7 years ago

What do you think about this small newline format combinator?

let nl = Format.pp_print_newline

Which can be used like this:

# Fmt.(pr "%a" (list ~sep:nl int) [1; 2; 3])
1
2
3
dbuenzli commented 7 years ago

What do you think about this small newline format combinator?

I think it's bad, that's not the way formatters should be used, it kills compositionality (see also #26). The way of writing what you want is by using a vertical box:

Fmt.(pr "@[<v>%a@]" (list int) [1; 2; 3]);;
1                                                                                                                                      
2                                                                                                                                      
3

or

# Fmt.(pr "%a" (vbox @@ list int) [1; 2; 3]);;
1                                                                                                                                      
2                                                                                                                                      
3
rizo commented 1 year ago

On a related note, before I open a new issue...

Would a Fmt.pl and Fmt.epl be acceptable for alternatives to Fmt.pr and Fmt.epr that append @. to the format?

I've seen people (myself including) forget to add @. way too many times and then wonder why the output is not flushed.

Unlike the originally proposed combinator, this one does not break composition as it only happens at the top-level.

Happy to submit a PR if you're ok with this addition.

dbuenzli commented 1 year ago

Wasn't that discussed in https://github.com/dbuenzli/fmt/issues/26 ?

rizo commented 1 year ago

Missed that one. After reviewing the thread: