OCaml : why comparison operator are type agnostic, whereas arithmetic ones are not?

后端 未结 2 1549
不知归路
不知归路 2021-01-12 04:55

I am wondering why < operator supports int, string, bool, or float, whereas + only support int.

Could OCaml recognize the underlying arithmetic to use ? What mak

2条回答
  •  情话喂你
    2021-01-12 05:31

    There is not, at the moment, a notion of "a bit polymorphic" in OCaml (the technical name is "ad-hoc polymorphism"). You can't say "I accept integers and floats but not the rest".

    You can say, however "I accept everything", just like the comparison operator (the technical name is "parametric polymorphism"). Do note however than they are lying a bit about that: you can't compare functions, even if the type system can't catch it.

    See this answer for details.

提交回复
热议问题