Statically resolved string conversion function in F#
f#
Solution
This seems to work:
type Conversions = Conversions with
static member ($) (Conversions, value: int) = value.ToString()
static member ($) (Conversions, value: bool) = value.ToString()
let inline conv value = Conversions $ value
conv 1 |> ignore
conv true |> ignore
conv "foo" |> ignore //won't compile
Problem
I'm trying to create a function in F# that will convert certain types to a string, but not others. The objective is so that a primitive can be passed but a complex object cannot be passed by accident. Here's what I have so far: ``` type Conversions = static member Convert (value:int) = value.ToString() static member Convert (value:bool) = value.ToString() let inline convHelper< ^t, ^v when ^t : (static member Convert : ^v -> string) > (value:^v) = ( ^t : (static member Convert : ^v -> string) (value)) let inline conv (value:^v) = convHelper<Conversions, ^v>(value) ``` Unfortunately, my `conv` function gets the following compile-time error: ``` A unique overload for method 'Convert' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: static member Conversions.Convert : value:bool -> string, static member Conversions.Convert : value:int -> string ``` What am I doing wrong?