Snippets created by Anton Tcholakov

  • Posting async requests to an unreliable agent with error handling

    This example shows an implementation a MailboxProcessor<'T> extension method for posting messages and awaiting an asynchronous reply from a mailbox processor agent which mail fail. The error continuation is called if a failure event is triggered on the agent after the message is posted. The agent goes into a failed state where the result of all future messages posted with this mechanism is failure.

    2 people like this

    Posted: 2 years ago by Anton Tcholakov

  • "Guard" helper function for railway error handling

    Defines a "guard" function for railway-style error handing which allows you to concisely verify a condition when handling errors using Choice<'T, 'Error>. It checks the condition and if it is false, returns Choice2Of2 with the specified error value. If the condition is true then it returns Choice1Of2 (). Loosely inspired by Swift 2.0's guard keyword.

    2 people like this

    Posted: 2 years ago by Anton Tcholakov

  • Generic command/request agent with error handling

    Generic command/request agent with error handling used to serialise posted commands and requests which are defined by closures. Useful in serialising communications to an instrument with a C API.

    1 people like this

    Posted: 2 years ago by Anton Tcholakov

  • Deferred clean-up code (Go/Swift-style)

    Go and Swift have a dedicated defer keyword which allows the programmer to specify code which should run when leaving the current scope, irrespective of errors. F# supports a similar pattern with IDisposable and the use keyword, but this requires either defining a class definition or object expression and the extra syntax can make the code less expressive. This snippet defines a simple helper function called "defer" to remedy the situation.

    1 people like this

    Posted: 2 years ago by Anton Tcholakov

  • Async workflow with asynchronous "finally" clause

    The F# Core library offers async.TryFinally which where a synchronous compensation function (of type unit -> unit) is run after an error or cancellation. However, it offers no way to start an asynchronous compensation. The TryFinallyAsync method defined below offers a way around this.

    1 people like this

    Posted: 2 years ago by Anton Tcholakov

  • Generic command/request agent with error handling

    Generic command/request agent with error handling used to serialise posted commands and requests which are defined by closures. Useful in serialising communications to an instrument with a C API.

    3 people like this

    Posted: 2 years ago by Anton Tcholakov