1

The title says it.

In OCaml, you can do 1 + 1 but not 1.0 + 1.0

I kinda get this, '+' is a function that takes two int argument.

But why do we have '>' that works for float and int?

Is this inconsistency?

Aaron
  • 41
  • 4
  • Same reason as every other questionable thing in every language: language design is *hard*. – Jared Smith Sep 05 '18 at 00:30
  • To expand on that link a bit, I don't know of a way to specialize polymorphic operators. You can't say 'if `'a` is integer, call this function, and if `'a` is float, call a different one' in the ocaml type system without getting into things like functors. It would require compiler magic, and I assume the devs just never wanted to add it for arithmetic operators. – Shawn Sep 05 '18 at 00:41

1 Answers1

4

Well, note that '>' works for two values of any one type. It's not just for numbers.

But yes, the polymorphic comparison operators are a kind of special case in OCaml. Theoretically it shouldn't be possible to define such functions, as there's no a priori way to "look inside" values with arbitrary types. However the functions exist anyway because they're extremely useful.

They are also somewhat dangerous, and many OCaml experts advise being very careful with them.

There is some discussion of the limitations here: Does compare work for all types?

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108