What type is this?

I just started working through the book Tamizhvendan S 'F # I applied and came across this piece of code:

type WebPart = Context -> Async<Context option> 

I looked at Microsoft F # docs as well as my favorite F # Scott Wlaschin F# for Fun and Profit site, but could not find a link to this type. This is not like a post type. It almost looks like a plain old function. So what is it?

Thanks for any help to the tacks and guys.

+5
source share
2 answers

The type of WebPart you are looking for comes from Suave . You can learn more about this at https://suave.io/async.html , but I will summarize. You are right: this is a type of function. In particular, 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 queries may fail, the function has the ability to return None instead of a value. And since some requests can take a long time, all requests are treated as returned asynchronously, which is why Async .

To bind the two queries together, Suave provides the binding operator >=> , so you do not have to go through the input template pattern async { match result1 with None -> None | Some x -> return! request2 x async { match result1 with None -> None | Some x -> return! request2 x async { match result1 with None -> None | Some x -> return! request2 x all the time; instead, you can simply type request1 >=> request2 for the same effect.

If you need more help, and reading Suave's documentation did not provide you with the help you need, let us know and we will try to explain further.

+12
source

It is too long to be a comment. And I never used Suave. However, I think that I can guess from the monadic properties of types that >=> not a binding operator, but a Claysley composition operator. Not observing the laws of the monad or trying to understand category theory , simply assuming that signatures should be:

 val ( >>= ) : Async<'a option> -> ('a -> Async<'b option>) -> Async<'b option> val ( >=> ) : ('a -> Async<'b option>) -> ('b -> Async<'c option>) -> 'a -> Async<'c option> 

The binding operator accepts Async<'a option> and converts it using the link function 'a -> Async<'b option> to Async<'b option> . The Claysley operator combines two functions of a binder into a third.

These operators work on monadic types and functions, which are more general than the specific types described by the abbreviation type type WebPart = Context -> Async<Context option> . The implementation of operators follows in an almost natural way.

 let (>>=) request1 binder = async { let! result1 = request1 match result1 with | None -> return None | Some x -> return! binder x } let (>=>) fga = fa >>= g 
+3
source

All Articles