Functions with generic parameter types

前端 未结 5 1568
傲寒
傲寒 2020-11-27 02:38

I am trying to figure out how to define a function that works on multiple types of parameters (e.g. int and int64). As I understand it, function overloading is not possible

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 03:18

    Overloading is typically the bugaboo of type-inferenced languages (at least when, like F#, the type system isn't powerful enough to contain type-classes). There are a number of choices you have in F#:

    • Use overloading on methods (members of a type), in which case overloading works much like as in other .Net languages (you can ad-hoc overload members, provided calls can be distinguished by the number/type of parameters)
    • Use "inline", "^", and static member constraints for ad-hoc overloading on functions (this is what most of the various math operators that need to work on int/float/etc.; the syntax here is weird, this is little-used apart from the F# library)
    • Simulate type classes by passing an extra dictionary-of-operations parameter (this is what INumeric does in one of the F# PowerPack libraries to generalize various Math algorithms for arbitrary user-defined types)
    • Fall back to dynamic typing (pass in an 'obj' parameter, do a dynamic type test, throw a runtime exception for bad type)

    For your particular example, I would probably just use method overloading:

    type MathOps =
        static member sqrt_int(x:int) = x |> float |> sqrt |> int
        static member sqrt_int(x:int64) = x |> float |> sqrt |> int64
    
    let x = MathOps.sqrt_int 9
    let y = MathOps.sqrt_int 100L
    

提交回复
热议问题