Type of addition (+) in F#

后端 未结 3 1630
猫巷女王i
猫巷女王i 2020-12-31 08:46

I just learned that OCAML have to have a . postfix for doing float arithmetic. An example would be 3. +. 4. which equals 7. (float). H

3条回答
  •  离开以前
    2020-12-31 09:21

    Briefly, F# has an ad-hoc-overloading mechanism via the inline keyword and "static member constraints". There is some further magic specific to the built-in math operators, which magically assumes type int the absence of other constraints. (+) is just about the most special/magical thing in all of F#, so it does not make for a nice introduction to the language/type system.

    In general, "overloading" is difficult for statically-typed, type-inferred languages. F#'s choices here are very pragmatic. OCaml does a different, simple, pragmatic thing (no overloading). Haskell does a different, complex-but-elegant thing (type classes). They're all somewhat reasonable points in the language/library design space.

提交回复
热议问题