F# 将值向上转换为通配符意味着什么?
在case语句末尾实现通配符意味着什么 采用以下语法:F# 将值向上转换为通配符意味着什么?,f#,F#,在case语句末尾实现通配符意味着什么 采用以下语法: match imp req with | Success () -> this.Ok () :> _ | Success () -> this.Ok () :> IHttpActionResult type PushController (imp) = inherit ApiController () member this.Post (portalId : string, req : Push
match imp req with
| Success () -> this.Ok () :> _
| Success () -> this.Ok () :> 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) :> _
这与:
match imp req with
| Success () -> this.Ok () :> _
| Success () -> this.Ok () :> 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) :> _
编写这种语法有什么好处
以下是我问题的背景:
match imp req with
| Success () -> this.Ok () :> _
| Success () -> this.Ok () :> 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) :> _
运算符
:>
对其右侧表达式指定的类型执行静态向上转换。此运算符的语法为:
:>表达式
以你为例,这将是:
some_value :> IHttpActionResult
这告诉编译器,某个值
实际上是实现IHttpActionResult
的对象
但根据F#文件:
当您使用向上转换运算符时,编译器将尝试推断
从上下文转换为的类型。如果编译器无法
要确定目标类型,编译器将报告一个错误
由于Post
方法可以返回的唯一类型是IHttpActionResult
,因此可以让编译器推断它
因此,在这方面:
:> _
相当于:
:> IHttpActionResult
这不是一个通配符,你只是让编译器根据你提供的
Post
签名推断类型转换。哦。。。那么,是否需要签名上的返回类型来推断此值?相关:是。我对此不是很肯定,但是|Success()->这个。Ok():>IHttpActionResult
可能允许您在签名中没有返回类型的情况下在其余情况下使用
。试一试。这里的upcast(this.Ok())
是否应该与指定的返回类型足够(对于另一种情况也是相同的原则),并且可能更清楚?