7

Given the following code:

type MyDU =
| B of bool
| I of int
| S of string

type Ops () =
    static member myFn<'T> x =
        let v =
            match x with
            | B b -> box b
            | I i -> box i
            | S s -> box s

        match v with
        | :? 'T as y -> Some y
        | _ -> None

... the following produce error FS0717: Unexpected type arguments:

I 7 |> Ops.myFn<int>

Ops.myFn<int> <| I 7

However, this works just fine:

Ops.myFn<int> (I 7)

Also, I get no errors if I define myFn in a module:

let myFn<'T> x =
    let v =
        match x with
        | B b -> box b
        | I i -> box i
        | S s -> box s

    match v with
    | :? 'T as y -> Some y
    | _ -> None

I 7 |> myFn<int>  // Works fine

Explanation?

MiloDC
  • 2,373
  • 1
  • 16
  • 25
  • 1
    The previous question is not _exact_ duplicte, because it talks about instance members (while yours is about static members), but I think the same answer applies. – Tomas Petricek Jan 09 '17 at 22:32
  • So you can use `I 7 |> (Ops.myFn : _ -> int option)` as stated in @TomasPetricek 's answer. It is strange behaviour when the usual functionality works from a module. You could also use a curried static member like this `static member myFnInt = (Ops.myFn : _ -> int option)`; though admittedly that defeats the purpose of the generics. – Luke Merrett Jan 09 '17 at 22:35

0 Answers0