Search code examples
f#suave

What kind of type is this?


I just started working through Tamizhvendan S' book F# Applied and came across this code snippet:

type WebPart = Context -> Async<Context option>

I've been looking through Microsoft's F# docs as well as my favorite F# site, Scott Wlaschin's F# for Fun and Profit but have been unable to find any reference to this kind of type. It doesn't seem like a record type. It almost seems like a plain old function. So what is it?

Thanks for any help gals and guys.


Solution

  • The WebPart type you're looking at comes from Suave. You can read more about it at https://suave.io/async.html, but I'll summarize. You're correct: it is a type for a function. Specifically, it is a function that takes a Context (which in Suave is a combination of an HTTP request record and a response record), and returns a Context option asynchronously. That is, since some requests can fail, the function has the option of returning None instead of a value. And since some requests can take a long time, all requests are treated as returning asynchronously, hence the Async.

    To chain two requests together, Suave provides the binding operator >=> so that you don't have to go through the boilerplate of typing async { match result1 with None -> None | Some x -> return! request2 x all the time; instead, you can just type request1 >=> request2 for the same effect.

    If you need more help and reading through the Suave documentation hasn't provided the help you need, let us know and we'll try to explain further.