1

For example, I have a workflow for logging, and I want to use async in the logging workflow. How to call logging's bind in async workflow?

log {
    async {
       let! a = .... // Need to do logging here.
       let! b = .... // Need to do async here

Edit: I heard that it's a good idea to use workflow to replace AOP for cross cutting concerns purpose in F#. I'm not sure how to handle the embedding issues. Or it's not a good idea to use workflow for it?

ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • Just out of purity, the native F# name for this construct is a computation expression rather than a monad – PiotrWolkowski Dec 08 '14 at 15:17
  • There was a similar question some time ago http://stackoverflow.com/questions/19129277/can-i-use-different-workflows-simultaneously-in-f – Gus Dec 08 '14 at 16:27

2 Answers2

5

F# does not have any automated way of doing this. What you essentially want is to create a composed computation that captures two different behaviors. The best way to do this is to define a new computation, say asyncLog, that lets you do both of the things you need. This is not easy, but the following are good starting points:

In reality, you do not need to do this that often in F#, because there are not that many computation expressions to compose.

In your example, you're using async and log. Although logging makes for a great demo when explaining how computation expressions work, I'm not sure that I would use computation expressions to add logging to my application in practice - it seems easier to just use a global logger object and call it directly (but it really depends on what your log computation does behind the covers).

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • I just use logging as an example here. (I heard that monad/computation expression is better than AOP for logging from an F# guy) As for composite computation, I may, take loggin/async example, only need to log some statements and async call some other statements, maybe both for some statements. Can it be done? – ca9163d9 Dec 08 '14 at 15:32
1

You can use AsyncReader computation expression for this https://github.com/jack-pappas/ExtCore/blob/master/ExtCore/Control.fs#L1769 and lift Asyncs and Readers if you need.