Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "Unexpected type arguments" mean?

Tags:

f#

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

like image 961
Remko Avatar asked Sep 09 '26 21:09

Remko


1 Answers

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>
like image 181
pad Avatar answered Sep 12 '26 16:09

pad