【发布时间】:2016-11-15 18:10:39
【问题描述】:
在 case 语句末尾实现通配符是什么意思?
采用以下语法:
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) :> _
【问题讨论】:
-
这不是通配符,您只是让编译器根据您提供的
Post签名为您推断类型转换。 -
哦...那么推断此签名所需的返回类型是什么?
-
是的。我对此并不肯定,但
| Success () -> this.Ok () :> IHttpActionResult可能允许您在其余情况下使用_,而签名中没有返回类型。试试看。 -
不应该
upcast (this.Ok ())足够在这里(和其他情况下的相同原则)作为返回类型被指定,也可能更清晰?
标签: f#