Last active
September 24, 2016 15:59
-
-
Save joeaudette/8cf717c97501f1dd73a7abf7a058ad3b to your computer and use it in GitHub Desktop.
fsharp help from stackoverflow 20160924
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this compiles and works: | |
[<HttpGet("{id}", Name = "GetFsToDo")>] | |
member __.Get(id) = | |
async { | |
let! data = Async.AwaitTask(__.Queries.Find(id)) | |
if isNull data | |
then return __.NotFound() :> IActionResult | |
else | |
return new ObjectResult(data) :> IActionResult } | |
|> Async.StartAsTask | |
when I changed it like this as suggested in comments: | |
[<HttpGet("{id}", Name = "GetFsToDo")>] | |
member __.Get(id) : IActionResult = | |
async { | |
let! data = Async.AwaitTask(__.Queries.Find(id)) | |
if isNull data | |
then return __.NotFound() :> _ | |
else | |
return new ObjectResult(data) :> _ } | |
|> Async.StartAsTask | |
or like this: | |
[<HttpGet("{id}", Name = "GetFsToDo")>] | |
member __.Get(id) : Task<IActionResult> = | |
async { | |
let! dataOrDefault = __.Queries.Find(id) |> Async.AwaitTask | |
match Option.ofObj dataOrDefault with | |
| None -> return __.NotFound() :> _ | |
| Some data -> return ObjectResult(data) :> _ | |
} |> Async.StartAsTask | |
I get | |
error FS0013: The static coercion from type | |
error FS0013: The static coercion from type | |
error FS0001: This expression was expected to have type | |
if I change it like this: | |
[<HttpGet("{id}", Name = "GetFsToDo")>] | |
member __.Get(id) : IActionResult = | |
async { | |
let! data = Async.AwaitTask(__.Queries.Find(id)) | |
if isNull data | |
then return __.NotFound() | |
else | |
return new ObjectResult(data) } | |
|> Async.StartAsTask | |
I get: | |
error FS0001: All branches of an 'if' expression must return the same type. This expression was expected to have type 'NotFoundResult' but here has type 'ObjectResult'. | |
error FS0001: This expression was expected to have type | |
it does work with the option like this: | |
[<HttpGet("{id}", Name = "GetFsToDo")>] | |
member __.Get(id) = | |
async { | |
let! dataOrDefault = __.Queries.Find(id) |> Async.AwaitTask | |
match Option.ofObj dataOrDefault with | |
| None -> return __.NotFound() :> IActionResult | |
| Some data -> return ObjectResult(data) :> IActionResult | |
} |> Async.StartAsTask | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks, but when I try that I still get the same compiler error, I've updated the gist to show what I tried which I think is exactly as you have shown.
I'm also unclear on the guidance about __ vs this and what circumstances to use one or the other, googling about that it seems there is no standard convention for choosing but use of "this" at least would be more expressive of what it is and more familiar for C# devs to read the code.