Skip to content
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

add Pp.verbatimf #18

Merged
merged 2 commits into from
Nov 16, 2023
Merged

add Pp.verbatimf #18

merged 2 commits into from
Nov 16, 2023

Conversation

mbarbin
Copy link
Collaborator

@mbarbin mbarbin commented Oct 31, 2023

Follow the same naming scheme as existing functions textf and paragraphf.

This function is meant to allow simplifying occurrences of the pattern Pp.verbatim (Printf.sprintf ...).

As a motivating example, you may refer to this file (although it is taken from a toy project).

Thank you for pp!

Follow the same naming scheme as existing functions textf and
paragraphf.

Signed-off-by: Mathieu Barbin <[email protected]>
@Alizter
Copy link
Contributor

Alizter commented Oct 31, 2023

Thanks for the PR! Looks like I got the type of Pp.paragraphf a little off and I was missing a changelog so I've gone ahead and added those in #19.

There I also added a format_string internal type which should allow us to enforce the correct type internally somewhat. Depending on which gets merged first we should do this for Pp.verbatimf also.

This seems like a sensible addition to me.

@mbarbin
Copy link
Collaborator Author

mbarbin commented Oct 31, 2023

Hi @Alizter,

Thank you for looking into it ! 😄

There I also added a format_string internal type which should allow us to enforce the correct type internally somewhat.

It's been a while since I looked at these highly polymorphic format parametrized types... 🧐

My impression is that the added format_string type annotation doesn't add new information. I'm basing this hunch on the evidence that when I highlight the parameter fmt from vscode using Merlin in a version of the tree where it is not annotated with your added format_string internal type, it still has the same type as format_string (('a, unit, string, 'tag t) format4). 🤔

Do you believe this type is not inferred from the fact that Printf.ksprintf is applied to a function that returns an 'a t?

Signed-off-by: Mathieu Barbin <[email protected]>
@Alizter
Copy link
Contributor

Alizter commented Oct 31, 2023

@mbarbin It will enforce the type of the format4 argument but not the return type. Previously my implementation of paragraphf was incorrect so annotating the arg can be an extra internal check. It should be inferred from Printf.ksprintf and your implementation is totally correct. It's just an extra safety measure.

@mbarbin
Copy link
Collaborator Author

mbarbin commented Oct 31, 2023

It will enforce the type of the format4 argument but not the return type.

In fact, I don't think this is this the case. The returned type of the full invocation is the last parameter of the format4, and it is correctly inferred as being 'a t.

I just looked again here.

I think the added annotation doesn't add any safety in that it only coerces the parameter with the same type as already inferred.

@Alizter
Copy link
Contributor

Alizter commented Oct 31, 2023

@mbarbin Yes, you're totally correct. I think what is needed is to add that type to the mli, then it would be able to enforce the correct type, but that has the side effect of polluting the mli which isn't desirable.

@mbarbin
Copy link
Collaborator Author

mbarbin commented Oct 31, 2023

Previously my implementation of paragraphf was incorrect so annotating the arg can be an extra internal check.

Ah, I think I now understand what you mean. OK, I believe having the correct type in the mli is enough to get a compile error on the previous implementation, so it should be all you need (and not need the added annotation in the ml).

@Alizter
Copy link
Contributor

Alizter commented Oct 31, 2023

I think a better way to enforce the correct type is just to add some simple tests like you've done.

@mbarbin
Copy link
Collaborator Author

mbarbin commented Oct 31, 2023

I think a better way to enforce the correct type is just to add some simple tests like you've done.

I agree yes. It's too easy to get it wrong otherwise!

Thanks for the follow through and for #19

@rgrinberg rgrinberg added this to the 1.3.0 milestone Nov 1, 2023
@rgrinberg rgrinberg merged commit 6749c14 into ocaml-dune:master Nov 16, 2023
7 checks passed
mbarbin added a commit to mbarbin/opam-repository-1 that referenced this pull request Sep 16, 2024
CHANGES:

- Prepare release (ocaml-dune/pp#21, @mbarbin)
  - Upgrade to `ocamlformat.0.26.2`.
  - Fmt the code
  - Add CI badge to README
  - Upgrade GitHub workflow actions dependencies (checkout@v4, setup-ocaml@v3)
  - Add more validation steps in CI
  - Add `ocamlformat` as dev-setup dependency

- Add `Pp.verbatimf`. (ocaml-dune/pp#18, @mbarbin)

- Add `Pp.paragraph` and `Pp.paragraphf` (ocaml-dune/pp#19, @Alizter)

- Remove `of_fmt` constructor. (ocaml-dune/pp#17, @Alizter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants