F# 编译错误:意外类型的应用程序 [英] F# compilation error: Unexpected type application

查看:22
本文介绍了F# 编译错误:意外类型的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 F# 中,给定以下类:

In F#, given the following class:

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

为什么下面会编译:

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

虽然以下不会编译:

let f = new Foo()
"string" |> f.Bar<Int32> //The compiler returns the error: "Unexpected type application"

推荐答案

看起来不支持在将方法视为第一类值时提供类型参数.我检查了 F# 规范 和这里是一些重要的部分:

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 项目限定查找
[如果应用程序表达式以:]

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

  • expr,然后使用 作为类型参数和 expr 作为表达方式论证.
  • expr,然后使用 expr 作为表达式参数.
  • 否则不使用表达式参数或类型参数.
  • 如果 [method] 被标记为RequiresExplicitTypeArguments 属性然后显式类型参数必须具有已经给了.
  • <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>

这篇关于F# 编译错误:意外类型的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆