F# compilation error: Unexpected type application

前端 未结 1 559
攒了一身酷
攒了一身酷 2020-12-17 20:41

In F#, given the following class:

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

Why does the following compile:

相关标签:
1条回答
  • 2020-12-17 21:46

    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>
    
    0 讨论(0)
提交回复
热议问题