LanguageExt.Core

LanguageExt.Core Effects IO

Contents

Sub modules

DSL
Extensions
Operators
Prelude

class EnvIO Source #

Environment for the IO monad

Fields

field Resources Resources Source #

field CancellationToken Token Source #

field CancellationTokenSource Source Source #

field SynchronizationContext? SyncContext Source #

Properties

property EnvIO Local Source #

property EnvIO LocalResources Source #

property EnvIO LocalCancel Source #

property EnvIO LocalSyncContext Source #

Methods

method EnvIO New ( Resources? resources = null, CancellationToken token = default, CancellationTokenSource? source = null, SynchronizationContext? syncContext = null) Source #

method void Dispose () Source #

method string ToString () Source #

record ForkIO <A> ( IO<Unit> Cancel, IO<A> Await) Source #

Result of forking an IO monad

Parameters

type A

Bound value type

param Cancel

An IO monad, which, if invoked, would cancel the forked IO operation

param Await

An IO monad, which, if invoked, would await the result of the forked IO operation. Obviously, this mitigates the reasons for forking somewhat, but this struct could be passed to another process that does the awaiting - and so still has some value.

record IO <A> Source #

A value of type IO is a computation which, when performed, does some I/O before returning a value of type A.

There is really only one way you should "perform" an I/O action: bind it to Main in your program: When your program is run, the I/O will be performed. It shouldn't be possible to perform I/O from an arbitrary function, unless that function is itself in the IO monad and called at some point, directly or indirectly, from Main.

As this is C#, the above restrictions are for you to enforce. It would be reasonable to relax that approach and have I/O invoked from, say, web-request handlers - or any other 'edges' of your application.

IO is a monad, so IO actions can be combined using either the LINQ-notation or the bind operations from the Monad class.

Parameters

type A

Bound value

param runIO

The lifted thunk that is the IO operation

Properties

property IO<A> Empty Source #

Methods

method IO<B> Map <B> (Func<A, B> f) Source #

method IO<B> ApplyBack <B> (K<IO, Func<A, B>> f) Source #

method IO<B> Map <B> (B value) Source #

method IO<A> MapFail (Func<Error, Error> f) Source #

method IO<B> BiMap <B> (Func<A, B> Succ, Func<Error, Error> Fail) Source #

method IO<B> Match <B> (Func<A, B> Succ, Func<Error, B> Fail) Source #

method IO<A> IfFail (Func<Error, A> Fail) Source #

method IO<A> IfFail (A Fail) Source #

method IO<A> IfFail (Func<Error, IO<A>> Fail) Source #

method IO<A> IfFail (IO<A> Fail) Source #

method IO<S> Fold <S> ( Schedule schedule, S initialState, Func<S, A, S> folder) Source #

method IO<S> Fold <S> ( S initialState, Func<S, A, S> folder) Source #

method IO<S> FoldWhile <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<S, bool> stateIs) Source #

method IO<S> FoldWhile <S> ( S initialState, Func<S, A, S> folder, Func<S, bool> stateIs) Source #

method IO<S> FoldWhile <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<A, bool> valueIs) Source #

method IO<S> FoldWhile <S> ( S initialState, Func<S, A, S> folder, Func<A, bool> valueIs) Source #

method IO<S> FoldWhile <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<(S State, A Value), bool> predicate) Source #

method IO<S> FoldWhile <S> ( S initialState, Func<S, A, S> folder, Func<(S State, A Value), bool> predicate) Source #

method IO<S> FoldUntil <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<S, bool> stateIs) Source #

method IO<S> FoldUntil <S> ( S initialState, Func<S, A, S> folder, Func<S, bool> stateIs) Source #

method IO<S> FoldUntil <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<A, bool> valueIs) Source #

method IO<S> FoldUntil <S> ( S initialState, Func<S, A, S> folder, Func<A, bool> valueIs) Source #

method IO<S> FoldUntil <S> ( S initialState, Func<S, A, S> folder, Func<(S State, A Value), bool> predicate) Source #

method IO<S> FoldUntil <S> ( Schedule schedule, S initialState, Func<S, A, S> folder, Func<(S State, A Value), bool> predicate) Source #

method IO<A> Post () Source #

Make this IO computation run on the SynchronizationContext that was captured at the start of the IO chain (i.e. the one embedded within the EnvIO environment that is passed through all IO computations)

method IO<B> Bind <B> (Func<A, K<IO, B>> f) Source #

method IO<B> BindAsync <B> (Func<A, ValueTask<K<IO, B>>> f) Source #

method IO<B> Bind <B> (Func<A, IO<B>> f) Source #

method IO<B> Bind <B> (Func<A, Pure<B>> f) Source #

method K<M, B> Bind <M, B> (Func<A, K<M, B>> f) Source #

where M : Monad<M>

method IO<B> BindAsync <B> (Func<A, ValueTask<IO<B>>> f) Source #

method K<M, B> BindAsync <M, B> (Func<A, ValueTask<K<M, B>>> f) Source #

where M : Monad<M>

method IO<B> Select <B> (Func<A, B> f) Source #

method IO<C> SelectMany <B, C> (Func<A, IO<B>> bind, Func<A, B, C> project) Source #

method IO<C> SelectMany <B, C> (Func<A, K<IO, B>> bind, Func<A, B, C> project) Source #

method IO<C> SelectMany <B, C> (Func<A, Pure<B>> bind, Func<A, B, C> project) Source #

method Eff<C> SelectMany <B, C> (Func<A, Eff<B>> bind, Func<A, B, C> project) Source #

method Eff<RT, C> SelectMany <RT, B, C> (Func<A, Eff<RT, B>> bind, Func<A, B, C> project) Source #

method IO<C> SelectMany <C> (Func<A, Guard<Error, Unit>> bind, Func<A, Unit, C> project) Source #

method IO<A> Bracket () Source #

The IO monad tracks resources automatically, this creates a local resource environment to run this computation in. Once the computation has completed any resources acquired are automatically released. Imagine this as the ultimate using statement.

method IO<A> BracketFail () Source #

The IO monad tracks resources automatically, this creates a local resource environment to run this computation in. If the computation errors then the resources are automatically released.

This differs from Bracket in that Bracket will also free resources once the computation is successfully complete. BracketFail only frees resources on failure.

method IO<C> Bracket <B, C> (Func<A, IO<C>> Use, Func<A, IO<B>> Fin) Source #

When acquiring, using, and releasing various resources, it can be quite convenient to write a function to manage the acquisition and releasing, taking a function of the acquired value that specifies an action to be performed in between.

Parameters

param Use

Function to use the acquired resource

param Fin

Function to invoke to release the resource

method IO<C> Bracket <B, C> (Func<A, IO<C>> Use, Func<Error, IO<C>> Catch, Func<A, IO<B>> Fin) Source #

When acquiring, using, and releasing various resources, it can be quite convenient to write a function to manage the acquisition and releasing, taking a function of the acquired value that specifies an action to be performed in between.

Parameters

param Use

Function to use the acquired resource

param Catch

Function to run to handle any exceptions

param Fin

Function to invoke to release the resource

method IO<A> Timeout (TimeSpan timeout) Source #

Applies a time limit to the IO computation. If exceeded an exception is thrown.

Parameters

param timeout

Timeout

returns

Result of the operation or throws if the time limit exceeded.

method IO<A> Local () Source #

Create a local cancellation environment

method IO<ForkIO<A>> Fork (Option<TimeSpan> timeout = default) Source #

Queues the specified work to run on the thread pool

Any resources acquired within a forked IO computation will automatically be released upon the forked computation's completion (successful or otherwise). Resources acquired in the parent thread will be available to the forked thread, and can be released from there, but they are shared resources at that point and should be treated with care.

Parameters

param timeout

Optional timeout

returns

Fork record that contains members for cancellation and optional awaiting

method FinT<IO, A> Try () Source #

Run the IO monad to get its result. Differs from Run in that it catches any exceptions and turns them into a Fin〈A〉 result.

method IO<A> Repeat () Source #

Keeps repeating the computation forever, or until an error occurs

Any resources acquired within a repeated IO computation will automatically be released. This also means you can't acquire resources and return them from within a repeated computation.

Parameters

returns

The result of the last invocation

method IO<A> Repeat (Schedule schedule) Source #

Keeps repeating the computation, until the scheduler expires, or an error occurs

Any resources acquired within a repeated IO computation will automatically be released. This also means you can't acquire resources and return them from within a repeated computation.

Parameters

param schedule

Scheduler strategy for repeating

returns

The result of the last invocation

method IO<A> RepeatWhile (Func<A, bool> predicate) Source #

Keeps repeating the computation until the predicate returns false, or an error occurs

Any resources acquired within a repeated IO computation will automatically be released. This also means you can't acquire resources and return them from within a repeated computation.

Parameters

param predicate

Keep repeating while this predicate returns true for each computed value

returns

The result of the last invocation

method IO<A> RepeatWhile ( Schedule schedule, Func<A, bool> predicate) Source #

Keeps repeating the computation, until the scheduler expires, or the predicate returns false, or an error occurs

Any resources acquired within a repeated IO computation will automatically be released. This also means you can't acquire resources and return them from within a repeated computation.

Parameters

param schedule

Scheduler strategy for repeating

param predicate

Keep repeating while this predicate returns true for each computed value

returns

The result of the last invocation

method IO<A> RepeatUntil (Func<A, bool> predicate) Source #

Keeps repeating the computation until the predicate returns true, or an error occurs

Any resources acquired within a repeated IO computation will automatically be released. This also means you can't acquire resources and return them from within a repeated computation.

Parameters

param predicate

Keep repeating until this predicate returns true for each computed value

returns

The result of the last invocation

method IO<A> RepeatUntil ( Schedule schedule, Func<A, bool> predicate) Source #

Keeps repeating the computation, until the scheduler expires, or the predicate returns true, or an error occurs

Any resources acquired within a repeated IO computation will automatically be released. This also means you can't acquire resources and return them from within a repeated computation.

Parameters

param schedule

Scheduler strategy for repeating

param predicate

Keep repeating until this predicate returns true for each computed value

returns

The result of the last invocation

method IO<A> Retry () Source #

Retry if the IO computation fails

This variant will retry forever

Any resources acquired within a retrying IO computation will automatically be released if the operation fails. So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that acquires resources will have them tracked in the usual way.

method IO<A> Retry (Schedule schedule) Source #

Retry if the IO computation fails

This variant will retry until the schedule expires

Any resources acquired within a retrying IO computation will automatically be released if the operation fails. So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that acquires resources will have them tracked in the usual way.

method IO<A> RetryWhile (Func<Error, bool> predicate) Source #

Retry if the IO computation fails

This variant will keep retrying whilst the predicate returns true for the error generated at each iteration; at which point the last raised error will be thrown.

Any resources acquired within a retrying IO computation will automatically be released if the operation fails. So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that acquires resources will have them tracked in the usual way.

method IO<A> RetryWhile ( Schedule schedule, Func<Error, bool> predicate) Source #

Retry if the IO computation fails

This variant will keep retrying whilst the predicate returns true for the error generated at each iteration; or, until the schedule expires; at which point the last raised error will be thrown.

Any resources acquired within a retrying IO computation will automatically be released if the operation fails. So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that acquires resources will have them tracked in the usual way.

method IO<A> RetryUntil (Func<Error, bool> predicate) Source #

Retry if the IO computation fails

This variant will keep retrying until the predicate returns true for the error generated at each iteration; at which point the last raised error will be thrown.

Any resources acquired within a retrying IO computation will automatically be released if the operation fails. So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that acquires resources will have them tracked in the usual way.

method IO<A> RetryUntil (Schedule schedule, Func<Error, bool> predicate) Source #

Retry if the IO computation fails

This variant will keep retrying until the predicate returns true for the error generated at each iteration; or, until the schedule expires; at which point the last raised error will be thrown.

Any resources acquired within a retrying IO computation will automatically be released if the operation fails. So, successive retries will not grow the acquired resources on each retry iteration. Any successful operation that acquires resources will have them tracked in the usual way.

method IO<A> Catch (Func<Error, bool> Predicate, Func<Error, K<IO, A>> Fail) Source #

Catches any error thrown by invoking this IO computation, passes it through a predicate, and if that returns true, returns the result of invoking the Fail function, otherwise this is returned.

Parameters

param Predicate

Predicate

param Fail

Fail functions

method IO<A> Finally <X> (K<IO, X> @finally) Source #

Run a finally operation after the this operation regardless of whether this succeeds or not.

Parameters

param finally

Finally operation

returns

Result of primary operation

method IO<A> Combine (IO<A> rhs) Source #

Monoid combine

Parameters

param rhs

Alternative

returns

This if computation runs without error. rhs otherwise

method A Run () Source #

Run the IO monad to get its result

Any lifted asynchronous operations will yield to the thread-scheduler, allowing other queued operations to run concurrently. So, even though this call isn't awaitable it still plays nicely and doesn't block the thread.

NOTE: An exception will always be thrown if the IO operation fails. Lift this monad into other error handling monads to leverage more declarative error handling.

Parameters

param env

IO environment

returns

Result of the IO operation

method A Run (EnvIO envIO) Source #

Run the IO monad to get its result

Any lifted asynchronous operations will yield to the thread-scheduler, allowing other queued operations to run concurrently. So, even though this call isn't awaitable it still plays nicely and doesn't block the thread.

NOTE: An exception will always be thrown if the IO operation fails. Lift this monad into other error handling monads to leverage more declarative error handling.

Parameters

param env

IO environment

returns

Result of the IO operation

method ValueTask<A> RunAsync () Source #

Run the IO monad to get its result

This forks the operation to run on a new task that is then awaited.

Any lifted asynchronous operations will yield to the thread-scheduler, allowing other queued operations to run concurrently. So, even though this call isn't awaitable it still plays nicely and doesn't block the thread.

NOTE: An exception will always be thrown if the IO operation fails. Lift this monad into other error handling monads to leverage more declarative error handling.

Parameters

returns

Result of the IO operation

method ValueTask<A> RunAsync (EnvIO envIO) Source #

Run the IO monad to get its result

This forks the operation to run on a new task that is then awaited.

Any lifted asynchronous operations will yield to the thread-scheduler, allowing other queued operations to run concurrently. So, even though this call isn't awaitable it still plays nicely and doesn't block the thread.

NOTE: An exception will always be thrown if the IO operation fails. Lift this monad into other error handling monads to leverage more declarative error handling.

Parameters

returns

Result of the IO operation

method string ToString () Source #

class IO Source #

Fields

field IO<EnvIO> env = lift(e => e) Source #

field IO<CancellationTokenSource> source = lift(e => e.Source) Source #

field IO<Option<SynchronizationContext>> syncContext = lift(e => Optional(e.SyncContext)) Source #

Methods

method IO<A> pure <A> (A value) Source #

Lift a pure value into an IO computation

Parameters

type A

Bound value type

param value

value

returns

IO in a success state. Always yields the lifted value.

method IO<A> fail <A> (Error value) Source #

Put the IO into a failure state

Parameters

type A

Bound value type

param value

Error value

returns

IO in a failed state. Always yields an error.

method IO<A> fail <A> (string value) Source #

Put the IO into a failure state

Parameters

type A

Bound value type

param value

Error value

returns

IO in a failed state. Always yields an error.

method IO<Unit> lift (Action f) Source #

Lift an action into the IO monad

Parameters

param f

Action to lift

method IO<A> lift <A> (Either<Error, A> ma) Source #

method IO<A> lift <A> (Fin<A> ma) Source #

method IO<A> lift <A> (Func<A> f) Source #

method IO<A> lift <A> (Func<EnvIO, A> f) Source #

method IO<A> lift <A> (Func<Fin<A>> f) Source #

method IO<A> lift <A> (Func<EnvIO, Fin<A>> f) Source #

method IO<A> lift <A> (Func<Either<Error, A>> f) Source #

method IO<A> lift <A> (Func<EnvIO, Either<Error, A>> f) Source #

method IO<A> liftAsync <A> (Func<Task<A>> f) Source #

method IO<A> liftAsync <A> (Func<EnvIO, Task<A>> f) Source #

method IO<A> liftVAsync <A> (Func<ValueTask<A>> f) Source #

method IO<A> liftVAsync <A> (Func<EnvIO, ValueTask<A>> f) Source #

method K<M, A> local <M, A> (K<M, A> ma) Source #

where M : MonadUnliftIO<M>

Creates a local cancellation environment

A local cancellation environment stops other IO computations, that rely on the same environmental cancellation token, from being taken down by a regional cancellation.

If an IO.cancel is invoked locally, then it will still create an exception that propagates upwards and so catching cancellations is still important.

Parameters

type A

Bound value

param ma

Computation to run within the local context

returns

Result of the computation

method IO<A> local <A> (K<IO, A> ma) Source #

Creates a local cancellation environment

A local cancellation environment stops other IO computations, that rely on the same environmental cancellation token, from being taken down by a regional cancellation.

If an IO.cancel is invoked locally, then it will still create an exception that propagates upwards and so catching cancellations is still important.

Parameters

type A

Bound value

param ma

Computation to run within the local context

returns

Result of the computation

method IO<A> empty <A> () Source #

method IO<A> combine <A> (K<IO, A> ma, K<IO, A> mb) Source #

method K<M, B> mapIO <M, A, B> (K<M, A> ma, Func<IO<A>, IO<B>> f) Source #

where M : MonadUnliftIO<M>, Monad<M>

Queue this IO operation to run on the thread-pool.

Parameters

param timeout

Maximum time that the forked IO operation can run for. None for no timeout.

returns

Returns a ForkIO data-structure that contains two IO effects that can be used to either cancel the forked IO operation or to await the result of it.

method K<M, ForkIO<A>> fork <M, A> (K<M, A> ma, Option<TimeSpan> timeout = default) Source #

where M : MonadUnliftIO<M>

Queue this IO operation to run on the thread-pool.

Parameters

param timeout

Maximum time that the forked IO operation can run for. None for no timeout.

returns

Returns a ForkIO data-structure that contains two IO effects that can be used to either cancel the forked IO operation or to await the result of it.

method IO<Unit> yieldFor (Duration duration) Source #

Yield the thread for the specified duration or until cancelled.

Parameters

param duration

Amount of time to yield for

returns

Unit

method IO<Unit> yieldFor (TimeSpan timeSpan) Source #

Yield the thread for the specified duration or until cancelled.

Parameters

param timeSpan

Amount of time to yield for

returns

Unit

class IO Source #

class Resources Source #

Holds the acquired resources for the ResourceT monad transformer

Constructors

constructor Resources (Resources? parent) Source #

Methods

method IO<Resources> NewIO (Resources? parent) Source #

method void Dispose () Source #

method Unit DisposeU (EnvIO envIO) Source #

method Unit DisposeU () Source #

method IO<Unit> DisposeIO () Source #

method Unit Acquire <A> (A value) Source #

where A : IDisposable

method Unit AcquireAsync <A> (A value) Source #

where A : IAsyncDisposable

method Unit Acquire <A> (A value, Func<A, IO<Unit>> release) Source #

method IO<Unit> Release <A> (A value) Source #

method IO<Unit> ReleaseAll () Source #