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 a Compare module to the standard library #13755

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

kit-ty-kate
Copy link
Member

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 functions compare 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

stdlib/compare.ml Outdated Show resolved Hide resolved
external ( > ) : 'a -> 'a -> bool = "%greaterthan"
external ( <= ) : 'a -> 'a -> bool = "%lessequal"
external ( >= ) : 'a -> 'a -> bool = "%greaterequal"
end
Copy link
Contributor

@nojb nojb Feb 8, 2025

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).

Copy link
Member

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.

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.

5 participants