Skip to content

refactor Cmdliner usage from $ to let+ #503

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

v-gb
Copy link
Contributor

@v-gb v-gb commented Feb 6, 2025

Hi,

After the recent changes where I read/touched some command line definitions of the form:

let foo (`Term1 term1) (`Term2 term2) (`Term3 term3) = the_code
let term = Term.(const foo $ term1 $ term2 $ term3)

I propose to switch them all to:

let term = Term.(let+ term1 = term2 and+ term2 = term2 and+ term3 = term3 in the_code)

which is much simpler to modify/write. This is how everything is written in dune, AFAICT.

This is probably best read commit by commit. The last one may be more contentious. This is a fair amount of churn because of code moves, however the changes were made automatically (other than a couple of lines of diff in separate commits), so there's no risk of having swapped two arguments of type string. The code moves could potentially cause identifier captures, even though the code types and the tests pass, but the tool would warn about captures (for value identifiers. For module/types/constructors/etc, the result wouldd be virtually guaranteed to be a type error).

This is a change out of the blue, but I wanted to try out automating this change regardless of whether you want to take it, so if you don't want this, I'm fine with closing.

$ Cli.dev_repo)
let open Syntax in
let+ () = Cli.setup
and+ dry_run = Cli.dry_run
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use let open Cli and then and+ dry_run and+ pkg_names .... That's available starting from OCaml 4.13 and ocamlformat will auto-format it that way if you let it :-)

I'm normally not a fan of random opens but in this case might be ok (or move all the things that are meant to be opened into a submodule O and let open Cli.O in to avoid pulling in unexpected names.

Copy link
Contributor Author

@v-gb v-gb Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want the change, I suppose I can do that while I'm at it.
But if it's merely a thought you had, I wouldn't add this kind of open in my own code (in fact I don't even want to open Cmdliner.Term in my code).

(** A [--no-auto-open] option to disable opening of the opam-repository PR in
the browser. *)

val version : Dune_release.Version.t Arg.conv
(** [version] is a converter for parsing version specifiers *)

val pkg_names : [ `Package_names of string list ] Term.t
val pkg_names : string list Term.t
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will say that this is quite a bit nicer, I like it.

@Leonidas-from-XIV Leonidas-from-XIV added the no changelog Add this to your PRs to disable the changelog update check label Feb 7, 2025
@v-gb v-gb force-pushed the refactor-cmdliner branch from 7ee17d5 to 5f2f47e Compare February 7, 2025 09:25
@v-gb v-gb force-pushed the refactor-cmdliner branch from 5f2f47e to ca2ce09 Compare February 28, 2025 20:39
@v-gb
Copy link
Contributor Author

v-gb commented Feb 28, 2025

I pushed a new version of the commits.
The interesting bit is not the contents of the commits (it's basically identical), it's the fact that ocamlmig would now warn if a value identifier didn't resolve the same way before/after the code moves [1], and it didn't. So the risk of introducing a problem is this much lower.

[1] concretely, since there was no opportunity for a bug in the code, I added one in 96663bf, and one can see what happens to _x in ef6c5f0.

v-gb added 5 commits March 4, 2025 23:35
This change was made purely mechanically, with:

ocamlmig replace \
  -e 'const [%move_def __f] /// const __f' \
  -e 'const (fun __p1 __p2 __p3 -> __body) $ __e1 $ __e2 $ __e3
      /// let open Syntax in let+ __p1 = __e1 and+ __p2 = __e2 and+ __p3 = __e3 in __body' \
  -w
These are only necessary because the $ interface is so error prone otherwise.
Since we don't use the $ interface anymore, these tags are unnecessary.

This change is purely mechanical, and made with:

ocamlmig replace \
  bin/*.ml bin/cli.mli \ 
  -e '(Cli.named or named) __ __e /// __e' \
  -e 'type: [ `X__ of __t ] /// __t' \
  -e 'type: [> `X__ of __t ] /// __t' \
  -e 'binding: let+ `X__ __pat = __e in __ /// let+ __pat = __e in __' \
  -w
I think this style is generally preferred. This change was made purely
mechanically with:

ocamlmig replace -e 'binding: let+ __pat = [%move_def __f] in __ /// let+ __pat = __f in __' -w
@v-gb v-gb force-pushed the refactor-cmdliner branch from ca2ce09 to d8b98ca Compare March 4, 2025 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no changelog Add this to your PRs to disable the changelog update check
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants