Skip to content

Instantly share code, notes, and snippets.

@kevmal
Last active November 24, 2015 14:07
Show Gist options
  • Save kevmal/e817c763aa402e6746de to your computer and use it in GitHub Desktop.
Save kevmal/e817c763aa402e6746de to your computer and use it in GitHub Desktop.
Return type overloading to overload F# function
type A = A with static member Instance = A
type B = B with static member Instance = B
type C = C with static member Instance = C
type D = D with static member Instance = D
type E = E with static member Instance = E
let inline chooseType< ^a when ^a : (static member Instance : ^a) > = (^a : (static member Instance : ^a) ())
type F = F with
static member inline (?<-)(F,A,a) : int = a
static member inline (?<-)(F,B,a) : (int -> int) = fun i -> a + i
static member inline (?<-)(F,C,a) : (int -> string) = fun i -> a.ToString() + i.ToString()
static member inline (?<-)(F,D,a) : (int -> int -> int) = fun i j -> i + j + a
static member inline (?<-)(F,E,a:int) : (int -> double -> int) = fun i j -> ((double i)*j |> int) + a
let inline f t = (?<-) F chooseType t
printfn "%d" (f 1)
printfn "%d" (f 1 2)
printfn "%s" (f 1 2)
printfn "%d" (f 1 2 3)
printfn "%d" (f 1 2 3.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment