Skip to content

Instantly share code, notes, and snippets.

@joeaudette
Last active September 24, 2016 15:59
Show Gist options
  • Save joeaudette/8cf717c97501f1dd73a7abf7a058ad3b to your computer and use it in GitHub Desktop.
Save joeaudette/8cf717c97501f1dd73a7abf7a058ad3b to your computer and use it in GitHub Desktop.
fsharp help from stackoverflow 20160924
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
@bartelink
Copy link

bartelink commented Sep 24, 2016

You def need a cast somewhere, and you need to specify the type - the :> _ just lets you not redundantly state it, and F# will never just pick a common base (C# wouldn't either in a var-initialisation)

    [<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

@joeaudette
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment