-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 a Compare module to the standard library #13755
base: trunk
Are you sure you want to change the base?
Conversation
db04d84
to
0346afa
Compare
stdlib/compare.ml
Outdated
external ( > ) : 'a -> 'a -> bool = "%greaterthan" | ||
external ( <= ) : 'a -> 'a -> bool = "%lessequal" | ||
external ( >= ) : 'a -> 'a -> bool = "%greaterequal" | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am in two minds about these submodules.
The main use of them is with numeric types (Int
, Int64
, Char
, maybe String
, etc). If anything, monomorphic versions of these operators should be added to those modules. I don't think the operators <
, >
, <=
, >=
are very useful in contexts where one is explicitly using a polymorphic compare (since the ordering is implementation-defined). As for =
and <>
, they are useful, but once you have put them inside a submodule, I don't see myself using a local open to save a few character with respect to Compare.Poly.equal
.
In other words, perhaps it would be simpler not to include the Syntax
submodules at all (which would also mean that we do not have to discuss what to name the submodule).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words, perhaps it would be simpler not to include the Syntax submodules at all (which would also mean that we do not have to discuss what to name the submodule).
Yes, that would also be my preference to reduce decision fatigue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair enough, that works for me. Done.
0346afa
to
9d329b1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Docstrings need to be added.
And a second official approval is needed to move forward with this PR.
what do you think makes the most sense for this module? Should we copy the docstrings from |
Actually it may be nice to do things the other way around, that is, move the stdlib docstrings here and point to this new module in the Stdlib documentation. This paves the way for eventually deprecating |
I am not sure about the proposed API: For example, I thought of the following "flat" interface (I'm not saying I strongly prefer it, but I think that throwing some ideas around may help decide how we feel about the API):
We loose the information that
|
We could simply have |
In that case, better call the module "Poly". |
I think it'd be good to have a name than emphasizes the fact that the behaviour is defined on representations, i.e. that the functions are not parametrically polymorphic, despite their types. |
|
|
A twist on @nojb's proposal could be: Compare.repr : 'a -> 'a -> int
Compare.repr_equal : 'a -> 'a -> bool
Compare.phys_equal : 'a -> 'a -> bool
Compare.repr_min : 'a -> 'a -> 'a
Compare.repr_max : 'a -> 'a -> 'a |
I find the I'm happy to update the PR with the proposal that makes consensus. |
I think that there is a tension between having ominous-looking names that make people think twice about using polymorphic comparison, and having pleasant-looking names that encourage people to use them and gradually organize a transition out of In this context I personally find that |
9d329b1
to
9204c53
Compare
I've pushed two commits with the updated docstrings to help visualize the two alternative proposals.
|
Well it could be for operations on the representation of values "that you are allowed to use". For example So a synopsis could be:
|
One often-requested example: |
First proposed in #13753 (comment)
As previously extensively discussed in #9928, #9080 and in many tickets or forum posts, both the physical equality operator
==
and the polymorphic comparison functionscompare
and=
are known footguns for OCaml developers of all levels.Similarly to #9080 / #13753, this PR hopes to be a first step towards a world where OCaml does not have this footgun anymore by encouraging people to use functions from this new module (which is more explicit than using it directly from
Stdlib
and thus less prone to footgunning), then deprecating==
,compare
, … when they are less used.Note for reviewers: i'll add documentation to the functions once/if the interface is agreed