Type of addition (+) in F#

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

    In addition to Brian´s answer and link:

    https://github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/prim-types.fs

    I found some definitions in the code:

    let inline (+) (x:int) (y:int) = (# "add" x y : int #)
    

    and

    let inline (+) (x: ^T) (y: ^U) : ^V = 
         AdditionDynamic<(^T),(^U),(^V)>  x y 
         when ^T : int32       and ^U : int32      = (# "add" x y : int32 #)
         when ^T : float       and ^U : float      = (# "add" x y : float #)
         when ^T : float32     and ^U : float32    = (# "add" x y : float32 #)
         ...
    

    And the AdditionDynamic is defined here (loads of static stuff and CIL): https://github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/prim-types.fs#L2374

    Fun stuff:

    (# "add" 1 2 : int32 #)
    

    works, and gives 3 as output (with a warning saying you shouldn't do this.)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题