Type of addition (+) in F#

后端 未结 3 1656
猫巷女王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:23

    Overloaded functions (and operators) must be marked inline in F#. This is because they depend on explicit member constraints. Those constraints are resolved at compile time. A function let inline add a b = a + b has the type 'a -> 'b -> 'c (requires member (+)) where + is a static function/operator. You can't do this in C#; it doesn't have static member constraints.

    let inline add a b = a + b
    add 1 2 //works
    add 1.0 2.0 //also works
    

提交回复
热议问题