本文介绍了元组之后但另一类型之前的冒号在方法签名中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

位于元组之后但另一个类型之前的冒号是什么意思?

What does a colon that's positioned after a tuple but before another type mean within a method signature?

语法如下:

member this.Post (portalId : string, req : PushRequestDtr) : IHttpActionResult =

这里是上下文:

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) :> _

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

Specifically, what does this method signature mean?

此方法需要两个参数还是一个参数?

Does this method take two parameters or one parameter?

我理解这一点:

(portalId : string, req : PushRequestDtr)

但是我对附加在其末尾的语法感到困惑:

But I'm confused about this syntax that's appended to the end of it:

: IHttpActionResult

推荐答案

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

That would be the return type, i.e. the type of the value returned by the method.

从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

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

In this case return_type is IHttpActionResult, which means this method will return an object that implements the IHttpActionResult interface.

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

Also, although (portalId : string, req : PushRequestDtr) looks like a tuple (and in a certain way it is syntax-wise) it is not, in fact, treated as a tuple. In this case, this is a specific F# syntax for declaring method arguments while defining a method of a F# object. This is the part represented by method-nameparameter-list in the F# method template declaration. This means that the Post method receives two arguments: portalId and req, not a single argument as a tuple.

具体地说,在声明 method 参数而不是 function 参数时,必须使用看起来像元组但不是元组的参数列表的语法. member关键字是使此行成为 method 声明而不是 function 声明的关键词.

Specifically, this syntax of a list of arguments that looks like a tuple but that they are not a tuple has to be used when declaring method arguments instead of function arguments. The member keyword is the one that makes this line a method declaration instead of a function declaration.

-

关于:>运算符:这是强制转换运算符.更具体地说,是upcasting运算符(它将更多派生类型的类型更改为类型层次结构中某个更高类型的类型).

Regarding the :> operator: This is a cast operator. More specifically a upcasting operator (which changes the type of a more derived type to the type of some higher type in the type hierarchy).

在这种情况下,它用于显式告知编译器match表达式中的每个分支都将返回派生(或实现)IHttpActionResult的某种类型.我不太确定为什么要进行此强制转换(与F#在这种情况下无法推断正确的类型有关,请参见其他问题:),但实际上,它会将每个可能的返回值强制转换为方法的返回类型IHttpActionResult

In this case, it is being used to explicitly tell the compiler that each branch in the match expression will return some type that is derived (or that implements) IHttpActionResult. I am not quite sure why this cast is needed (something to do with F# not being able to infer the correct type in this context, see this other question: Type mismatch error. F# type inference fail?) but in fact, it is casting every possible return value to IHttpActionResult which is the method's return type.

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

这篇关于元组之后但另一类型之前的冒号在方法签名中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:48