Skip to content

Refactoring: Move to default optional parameters when they were reimplemented by hand #5806

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

Conversation

last-genius
Copy link
Contributor

Replaces patterns like this:

let start ?statefile ~xs ?ip domid =
    let ip = Option.value ~default:"127.0.0.1" ip in

With this:

let start ?statefile ~xs ?(ip = "127.0.0.1") domid =

All of the default values in this PR are literals, except for one case, which evaluates a function (whether it is or is not evaluated when a default value is provided is unspecified: https://ocaml.org/manual/5.2/expr.html#sss:expr-function-definition):

-let run ~__context ?mgmt_enabled () =
-  let mgmt_enabled = Option.value ~default:(mgmt_is_enabled ()) mgmt_enabled in
+let run ~__context ?(mgmt_enabled = mgmt_is_enabled ()) () =

It is equivalent to the existing code, however, since Option.value also evaluates the ~default arm.

=====

I didn't add a quality gate as it's quite hard to definitively grep for this case and it needs to be evaluated on an individual basis. I investigated adding semantic grepping, but this seems to be far away.

For posterity, here's the pattern I used to find these cases:

rg -U --pcre2 '\?(\w+)(.*\n){1,10}\s*let\s*\1 =\s*\n?(match|Option)'

Additionally, this PR removes Option.fold ~some:Fun.id that I found while working on this PR, replacing them with Option.value. These are the only occurrences of this pattern in the codebase.

=====

This change passed the BST+BVT test suites (Run 200710)

@last-genius last-genius marked this pull request as ready for review July 10, 2024 14:06
…ented by hand

Signed-off-by: Andrii Sultanov <andrii.sultanov@cloud.com>
@last-genius last-genius force-pushed the private/asultanov/default-optional-params branch from 6858a87 to 599fadd Compare July 10, 2024 14:09
Copy link
Contributor

@contificate contificate left a comment

Choose a reason for hiding this comment

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

Looks good to me.

I think in extreme cases this general transformation could somewhat impair readability but these cases look fine.

As for the unspecified behaviour w.r.t the default expr, it seems correct to be conservative and only provide an expression that you would be happy evaluating every time (regardless of whether an argument was provided) - so the specific case you've highlighted and rewritten looks fine to me.

There was internal commentary about using a type-safe IP representation, but that's probably for another PR (if you feel it is worth doing).

@contificate contificate merged commit 259fb4e into xapi-project:master Jul 15, 2024
15 checks passed
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