Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# compilation error: Unexpected type application

In F#, given the following class:

type Foo() =
    member this.Bar<'t> (arg0:string) = ignore()

Why does the following compile:

let f = new Foo()
f.Bar<Int32> "string"

While the following won't compile:

let f = new Foo()
"string" |> f.Bar<Int32> //The compiler returns the error: "Unexpected type application"
like image 253
Jim Burger Avatar asked Apr 30 '10 07:04

Jim Burger


1 Answers

It looks that providing type parameters when treating method as a first class value isn't supported. I checked the F# specification and here are some important bits:

14.2.2 Item-Qualified Lookup
[If the application expression begins with:]

  • <types> expr, then use <types> as the type arguments and expr as the expression argument.
  • expr, then use expr as the expression argument.
  • otherwise use no expression argument or type arguments.
  • If the [method] is labelled with the RequiresExplicitTypeArguments attribute then explicit type arguments must have been given.

If you specify type arguments and arguments, then the first case applies, but as you can see, the specification requires some actual arguments too. I'm not quite sure what is the motivation behind this, though.

Anyway, if you use the type parameter anywhere in the type signature of the member, then you can specify it using type annotations like this:

type Foo() = 
  member this.Bar<´T> (arg0:string) : ´T = 
    Unchecked.defaultof<´T>

let f = new Foo()
"string" |> (f.Bar : _ -> Int32)

On the other hand, if you don't use the type parameter anywhere in the signature, then I'm not quite sure why you need it in the first place. If you need it just for some runtime processing, then you may be able to take the runtime type representation as an argument:

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

let f = new Foo() 
"string" |> f.Bar typeof<Int32>
like image 199
Tomas Petricek Avatar answered Oct 20 '22 23:10

Tomas Petricek