在元组之后但在另一种类型之前的冒号意味着在方法签名中是什么意思?

Sco*_*rod 3 f#

在元组之后但在另一种类型之前定位的冒号在方法签名中意味着什么?

这是语法:

member this.Post (portalId : string, req : PushRequestDtr) : IHttpActionResult =
Run Code Online (Sandbox Code Playgroud)

这是上下文:

type PushController (imp) =
    inherit ApiController ()

    member this.Post (portalId : string, req : PushRequestDtr) : IHttpActionResult =
        match imp req with
        | Success () -> this.Ok () :> _
        | Failure (ValidationFailure msg) -> this.BadRequest msg :> _
        | Failure (IntegrationFailure msg) ->
            this.InternalServerError (InvalidOperationException msg) :> _
Run Code Online (Sandbox Code Playgroud)

具体来说,这种方法签名是什么意思?

此方法是采用两个参数还是一个参数?

我理解这个:

(portalId : string, req : PushRequestDtr)
Run Code Online (Sandbox Code Playgroud)

但是我对这个附加在它结尾的语法感到困惑:

: IHttpActionResult
Run Code Online (Sandbox Code Playgroud)

Ser*_*sta 7

这将是返回类型,即方法返回的值的类型.

来自F#在线文档:

https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/members/methods

// Instance method definition.
[ attributes ]
member [inline] self-identifier.method-nameparameter-list [ : return-type ]=
    method-body
Run Code Online (Sandbox Code Playgroud)

在这种情况下return_typeIHttpActionResult,这意味着该方法将返回一个实现的对象IHttpActionResult的接口.

此外,虽然(portalId : string, req : PushRequestDtr)看起来像一个元组(并且在某种程度上它是语法方面的),但事实上它并不被视为元组.在这种情况下,这是一种特定的F#语法,用于在定义F#对象的方法时声明方法参数.这是method-nameparameter-listF#方法模板声明中表示的部分.这意味着该Post方法接收两个参数:portalId并且req,不是单个参数作为元组.

具体来说,在声明方法参数而不是函数参数时,必须使用这种看起来像元组的参数列表的语法,但它们不是元组.member关键字是使该行成为方法声明而不是函数声明的关键字.

-

关于:>运营商:这是一个演员.更具体地说,是一个upcasting运算符(它将更多派生类型的类型更改为类型层次结构中某些更高类型的类型).

在这种情况下,它用于显式告诉编译器匹配表达式中的每个分支将返回一些派生(或实现)的类型IHttpActionResult.我不太清楚为什么需要这个强制转换(与F#无关,在这个上下文中无法推断出正确的类型,请参阅另一个问题:类型不匹配错误.F#类型推断失败?)但事实上,它正在转换每个可能的返回值IHttpActionResult是方法的返回类型.

https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/casting-and-conversions