When I try to curry a function with the last parameter as a generic type the compiler forces the generic type into the first concrete type it finds, thus losing its generic property for subsequent calls in scope. Example:
type Bar =
| String
| Integer
| Boolean
let foo (a: String)(b: 'm) =
printfn "%A - %A" a b
let foobar (a: String)(bar: Bar) =
let fooWithA= foo a
match bar with
| String -> fooWithA "String"
| Integer -> fooWithA 42
| Boolean -> fooWithA true
Here the last 2 lines give me a compiler error stating that the function is expecting a String. However, if I wrap the function with a helper class I can make it work like this:
type FooHelper(a:String) =
member this.foo (b: 'm) =
printfn "%A - %A" a b
let foobar (a: String)(bar: Bar) =
let fooHelperWithA= FooHelper a
match bar with
| String -> fooHelperWithA.foo "String"
| Integer -> fooHelperWithA.foo 42
| Boolean -> fooHelperWithA.foo true
That won't give me a compiler error. Then again if I try to us the function directly I get compilation error again:
let foobar (a: String)(bar: Bar) =
let fooHelperWithA= FooHelper(a).foo
match bar with
| String -> fooHelperWithA "String"
| Integer -> fooHelperWithA 42
| Boolean -> fooHelperWithA true
The last 2 lines throw a compilation error.
Is this an intended behavior or is this a bug? If this is how it should work, can someone explain why? this is really confusing to me.