3

I have the following code

module File1

    let convert<'T> x = x

    type myType () =
        member this.first<'T> y =
            convert<'T> y
        member this.second<'T> ys =
            ys
            |> Seq.map this.first<'T>

On the last 'T I get the error Unexpected type arguments. When for example I call let x = myType.first<int> "34" there are no warnings and everything works as expected. Leaving of the type argument gets rid of the warning and the program behaves as intended some of the time.

Can anyone explain what's going on here?

Thanks

Remko
  • 754
  • 3
  • 15

1 Answers1

4

In short, you need explicit arguments for your method with type arguments. The error can be fixed by changing

ys
|> Seq.map this.first<'T>

to

ys
|> Seq.map (fun y -> this.first<'T> y)

The error is explained very clearly in this excellent answer, I won't repeat it here. Notice that the error message has been changed between F# 2.0 and F# 3.0.

You don't actually use 'T anywhere in type signatures, so you can just remove 'T without any trouble.

If you need types for querying, I suggest to use the technique in Tomas' answer above.

type Foo() = 
  member this.Bar (t:Type) (arg0:string) = ()

let f = new Foo() 
"string" |> f.Bar typeof<Int32>
Community
  • 1
  • 1
pad
  • 41,040
  • 7
  • 92
  • 166