LayerUnion

Layer

Layer<'input, 'error, 'output> builds an environment or service bundle from an input value, running inside a Scope so acquired resources are finalized when the provided flow finishes. Read the composing layers guide for the layer { } provisioning walkthrough.

Combinator inventory

Function Use
Layer.succeed value A value that is already built
Layer.envWith projection A value read from the layer's input
Layer.fromFlow build Construction written as a flow: it can fail, read the input, and acquire scoped resources that live as long as the provided flow
Layer.acquireRelease acquire release A resource built by another layer, released when the provided flow ends
Layer.addFinalizer release Cleanup that runs when the provided flow ends
Layer.map, Layer.mapError, Layer.widenError Change the output or the error type
Layer.bind A step that depends on an earlier value, like let!
Layer.zip Two layers in sequence
Layer.zipPar, Layer.merge Two layers in parallel
Layer.map2, Layer.map3 Combine outputs without nested tuples

Composition rules

Layer.zip provisions left then right, sequentially. Layer.zipPar provisions both sides independently in parallel and returns a tuple. Layer.merge is the layer-domain name for zipPar; prefer it when combining service bundles or environment fragments. Layer.merge does not merge service contracts or synthesize a new environment type — it only provisions both sides and returns their outputs, so build the final environment yourself.

Layer error types must match the flow error type. When provisioning steps use different errors, map them into one startup error type with Layer.mapError before calling Layer.provide. A layer that cannot fail, such as BaseRuntime.live or Clock.layer, has error type Never; Layer.widenError gives it any error type.

Parallel composition uses parent-owned child scopes, so when one parallel branch fails after another acquired resources, the acquired resources are still released. If both parallel branches fail, Axial preserves both failures as Cause.Both (leftCause, rightCause) rather than discarding one side.

Specification

Kind
Union
Members
17
Examples
0
fromFlowSignature
Layer.fromFlow build
succeedSignature
Layer.succeed value
envWithSignature
Layer.envWith projection
addFinalizerSignature
Layer.addFinalizer release
acquireReleaseSignature
Layer.acquireRelease acquire release

Summary

NameSignatureSynopsis
Operations
fromFlowLayer.fromFlow buildCreates a layer that builds its output by running a flow with the layer's input as its environment.
succeedLayer.succeed valueCreates a layer that succeeds with a fixed output value.
envWithLayer.envWith projectionProjects part of the input environment into the layer output.
addFinalizerLayer.addFinalizer releaseRegisters a release flow with the layer scope.
acquireReleaseLayer.acquireRelease acquire releaseAcquires a resource and registers its release with the layer scope.
poolLayer.pool size acquireProvisions a fixed-size pool of resource instances, handed out round-robin.
mapLayer.map mapper layerMaps the successful output of a layer.
mapErrorLayer.mapError mapper layerMaps the typed provisioning failure of a layer.
widenErrorLayer.widenError layerGives a layer that cannot fail with a typed error any error type.
bindLayer.bind binder layerSequences layer provisioning with a dependent follow-up layer.
zipLayer.zip left rightBuilds two layers from the same input and scope and returns both outputs.
zipParLayer.zipPar left rightBuilds two independent layers in parallel and returns both outputs.
mergeLayer.merge left rightMerges two independent service layers in parallel.
map2Layer.map2 mapper left rightCombines two layers with a mapping function.
applyLayer.apply layer valueApplies a layer-wrapped function to a layer-wrapped value.
map3Layer.map3 mapper left middle rightCombines three layers with a mapping function.
provideLayer.provide layer flowBuilds an environment with a layer, runs a downstream flow, and always closes the layer scope.

Operations

kind:member

fromFlow

Layer.fromFlow build
Member
Creates a layer that builds its output by running a flow with the layer's input as its environment.

Parameters

NameTypeDescription
buildFlow<'input, 'error, 'output>The flow that builds the layer output.

Returns

Layer<'input, 'error, 'output>

Verification Examples

let database : Layer<AppConfig, DbError, Database> =
    Layer.fromFlow (Flow.scopeAcquireRelease (Database.connect ()) Database.close)
kind:member

succeed

Layer.succeed value
Member
Creates a layer that succeeds with a fixed output value.

Parameters

NameTypeDescription
value'output

Returns

Layer<'input, 'error, 'output>
kind:member

envWith

Layer.envWith projection
Member
Projects part of the input environment into the layer output.

Parameters

NameTypeDescription
projection'input -> 'output

Returns

Layer<'input, 'error, 'output>
kind:member

addFinalizer

Layer.addFinalizer release
Member
Registers a release flow with the layer scope.

Parameters

NameTypeDescription
releaseFlow<'input, Never, unit>The release flow run, with the layer's input as its environment, when the layer scope closes.

Returns

Layer<'input, 'error, unit>
kind:member

acquireRelease

Layer.acquireRelease acquire release
Member
Acquires a resource and registers its release with the layer scope.

Parameters

NameTypeDescription
acquireLayer<'input, 'error, 'resource>The layer that acquires the resource.
release'resource -> Flow<'input, Never, unit>The release flow run, with the layer's input as its environment, when the layer scope closes.

Returns

Layer<'input, 'error, 'resource>
kind:member

pool

Layer.pool size acquire
Member
Provisions a fixed-size pool of resource instances, handed out round-robin.

Parameters

NameTypeDescription
sizeintThe number of instances to provision. Must be at least one.
acquireint -> Layer<'input, 'error, 'resource>Builds one instance, given its zero-based index in the pool.

Returns

Layer<'input, 'error, Pool<'resource>>

Verification Examples

let checkerPool =
    Layer.pool 4 (fun _ -> Layer.succeed (FSharpChecker.Create(keepAssemblyContents = true)))
kind:member

map

Layer.map mapper layer
Member
Maps the successful output of a layer.

Parameters

NameTypeDescription
mapper'output -> 'next
layerLayer<'input, 'error, 'output>

Returns

Layer<'input, 'error, 'next>
kind:member

mapError

Layer.mapError mapper layer
Member
Maps the typed provisioning failure of a layer.

Parameters

NameTypeDescription
mapper'error -> 'nextError
layerLayer<'input, 'error, 'output>

Returns

Layer<'input, 'nextError, 'output>
kind:member

widenError

Layer.widenError layer
Member
Gives a layer that cannot fail with a typed error any error type.

Parameters

NameTypeDescription
layerLayer<'input, Never, 'output>

Returns

Layer<'input, 'error, 'output>

Verification Examples

let runtime : Layer<unit, string, int> = Layer.succeed 1 |> Layer.widenError
kind:member

bind

Layer.bind binder layer
Member
Sequences layer provisioning with a dependent follow-up layer.

Parameters

NameTypeDescription
binder'output -> Layer<'input, 'error, 'next>
layerLayer<'input, 'error, 'output>

Returns

Layer<'input, 'error, 'next>
kind:member

zip

Layer.zip left right
Member
Builds two layers from the same input and scope and returns both outputs.

Parameters

NameTypeDescription
leftLayer<'input, 'error, 'left>
rightLayer<'input, 'error, 'right>

Returns

Layer<'input, 'error, ('left * 'right)>
kind:member

zipPar

Layer.zipPar left right
Member
Builds two independent layers in parallel and returns both outputs.

Parameters

NameTypeDescription
leftLayer<'input, 'error, 'left>
rightLayer<'input, 'error, 'right>

Returns

Layer<'input, 'error, ('left * 'right)>
kind:member

merge

Layer.merge left right
Member
Merges two independent service layers in parallel.

Parameters

NameTypeDescription
leftLayer<'input, 'error, 'left>
rightLayer<'input, 'error, 'right>

Returns

Layer<'input, 'error, ('left * 'right)>
kind:member

map2

Layer.map2 mapper left right
Member
Combines two layers with a mapping function.

Parameters

NameTypeDescription
mapper'left -> 'right -> 'output
leftLayer<'input, 'error, 'left>
rightLayer<'input, 'error, 'right>

Returns

Layer<'input, 'error, 'output>
kind:member

apply

Layer.apply layer value
Member
Applies a layer-wrapped function to a layer-wrapped value.

Parameters

NameTypeDescription
layerLayer<'input, 'error, ('value -> 'next)>
valueLayer<'input, 'error, 'value>

Returns

Layer<'input, 'error, 'next>
kind:member

map3

Layer.map3 mapper left middle right
Member
Combines three layers with a mapping function.

Parameters

NameTypeDescription
mapper'left -> 'middle -> 'right -> 'output
leftLayer<'input, 'error, 'left>
middleLayer<'input, 'error, 'middle>
rightLayer<'input, 'error, 'right>

Returns

Layer<'input, 'error, 'output>
kind:member

provide

Layer.provide layer flow
Member
Builds an environment with a layer, runs a downstream flow, and always closes the layer scope.

Parameters

NameTypeDescription
layerLayer<'input, 'error, 'environment>The layer that builds the downstream environment.
flowFlow<'environment, 'error, 'value>The flow to run with the provided environment.

Returns

Flow<'input, 'error, 'value>

Verification Examples

let program () =
    let runtimeLayer = Layer.succeed "production"
    let workflow = Flow.envWith String.length
    Layer.provide runtimeLayer workflow