Repository F# setup
open System
open System.IO
open System.Threading
open System.Threading.Tasks
open Axial
open Axial.Layers
open Axial.Console
open Axial.FileSystem
open Axial.Hosting
open Axial.Hosting.Browser
open Axial.Hosting.Node
open Axial.PlatformService
open Axial.State
open Axial.Telemetry
open Axial.Telemetry.JavaScriptCreating Flows
Flow.succeed creates a description that succeeds with a value, and Flow.fail one that fails with an expected,
typed error. Neither runs until Flow.run starts it:
> let greeting () : Flow<string> = Flow.succeed "Hello";;
> Flow.run () (greeting ());;
> type LoadError = UserNotFound;;
> let missing () : Flow<LoadError, string> = Flow.fail UserNotFound;;
> Flow.run () (missing ());;val it: Exit<string,Never> = Success "Hello"
val it: Exit<string,LoadError> = Failure (Fail UserNotFound)greeting: unit -> Flow<string>FlowA flow that requires no environment and cannot fail with a typed error.
stringAn abbreviation for the CLI type . Basic Types
Axial.Flowsucceed: 'value -> Flow<'env,'error,'value>Same as ok. The value to wrap in a successful flow. A flow that always succeeds with the provided value. let result = Flow.succeed 42 |> Flow.run () // result = Success 42
run: 'env -> Flow<'env,'error,'value> -> Exit<'value,'error>Runs the workflow and blocks until the final exit is available. The environment used by the workflow. The workflow to run. The final workflow exit. let exit = workflow |> Flow.run environment
Core_03-the-flow-type_02-creating-flows.md_fsharp-0.LoadErrorUserNotFoundmissing: unit -> Flow<LoadError,string>FlowA flow that requires no environment and can fail with a typed error.
fail: 'error -> Flow<'env,'error,'value>Same as error. The error value to wrap in a failing flow. A flow that always fails with the provided error. let result = Flow.fail "error" |> Flow.run () // result = Failure (Cause.Fail "error")
Use Flow.fromTask or Flow.fromAsync when the operation comes from a Task- or Async-returning API and thrown
exceptions are defects. Use an attempt constructor when thrown exceptions are expected interop failures that callers
should handle:
> let path () = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "axial-docs-message.txt");;
> System.IO.File.WriteAllText(path (), "Hello from a file");;
> let readText (file: string) : Flow<string> = Flow.fromTask (fun token -> System.IO.File.ReadAllTextAsync(file, token));;
> Flow.run () (readText (path ()));;
> match Flow.run () (readText (path () + ".missing")) with Exit.Failure (Cause.Die error) -> error.GetType().Name | _ -> "";;
> let attemptRead (file: string) : ExnFlow<string> = Flow.attemptTask (fun token -> System.IO.File.ReadAllTextAsync(file, token));;
> match Flow.run () (attemptRead (path () + ".missing")) with Exit.Failure (Cause.Fail error) -> error.GetType().Name | _ -> "";;val it: unit = ()
val it: Exit<string,Never> = Success "Hello from a file"
val it: string = "FileNotFoundException"
val it: string = "FileNotFoundException"path: unit -> stringSystemCombine: string * string -> stringCombines two strings into a path. The first path to combine. The second path to combine. The combined paths. If one of the specified paths is a zero-length string, this method returns the other path. If contains an absolute path, this method returns . or contains one or more of the invalid characters defined in . or is .
IOSystem.IO.PathPerforms operations on instances that contain file or directory path information. These operations are performed in a cross-platform manner.
GetTempPath: unit -> stringReturns the path of the current user's temporary folder. The path to the temporary folder, ending with a backslash. The caller does not have the required permissions.
WriteAllText: string * string -> unitCreates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten. The file to write to. The string to write to the file. is a zero-length string, contains only white space, or contains one or more invalid characters as defined by . is . The specified path, file name, or both exceed the system-defined maximum length. The specified path is invalid (for example, it is on an unmapped drive). An I/O error occurred while opening the file. specified a file that is read-only. -or- specified a file that is hidden. -or- This operation is not supported on the current platform. -or- specified a directory. -or- The caller does not have the required permission. is in an invalid format. The caller does not have the required permission.
System.IO.FileProvides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of objects.
readText: string -> Flow<string>file: stringstringAn abbreviation for the CLI type . Basic Types
FlowA flow that requires no environment and cannot fail with a typed error.
Axial.FlowfromTask: (CancellationToken -> Task<'value>) -> Flow<'env,'error,'value>Creates a flow from a cancellable task factory. The factory runs on each execution and receives the runtime's cancellation token, so the flow stays cold and cancellable. Thrown exceptions are recorded as defects (Cause.Die). Use attemptTask when expected exceptions should enter the typed error channel. Starts the operation, observing the supplied cancellation token. .NET only let flow = Flow.fromTask (fun token -> client.GetStringAsync(url, token))
token: CancellationTokenReadAllTextAsync: string * CancellationToken -> Task<string>Asynchronously opens a text file, reads all the text in the file, and then closes the file. The file to open for reading. The token to monitor for cancellation requests. The default value is . A task that represents the asynchronous read operation, which wraps the string containing all text in the file.
run: 'env -> Flow<'env,'error,'value> -> Exit<'value,'error>Runs the workflow and blocks until the final exit is available. The environment used by the workflow. The workflow to run. The final workflow exit. let exit = workflow |> Flow.run environment
(+): ^T1 -> ^T2 -> ^T3Overloaded addition operator The first parameter. The second parameter. The result of the operation. 2 + 2 // Evaluates to 4 "Hello " + "World" // Evaluates to "Hello World"
Axial.Exit`2Represents the final outcome of a workflow execution. The type of the success value. The type of the domain-specific failure value.
FailureThe workflow failed due to a specific cause.
Axial.Cause`1Represents the cause of a failed workflow. The type of the domain-specific failure value.
DieAn unexpected defect or panic (e.g., an exception).
error: exnGetType: unit -> TypeGets the runtime type of the current instance. A object that represents the exact runtime type of the current instance.
Name: stringGets the name of the current member. A containing the name of this member.
attemptRead: string -> ExnFlow<string>ExnFlowA flow that requires no environment and uses exceptions as recoverable typed errors.
attemptTask: (CancellationToken -> Task<'value>) -> Flow<'env,exn,'value>Creates a flow from a cancellable task factory and treats thrown exceptions as recoverable typed errors. Successful completion returns Exit.Success. OperationCanceledException returns Cause.Interrupt when the runtime token requested it. Other exceptions, including cancellation the operation raised for its own reasons, return Cause.Fail exn. Starts the operation, observing the supplied cancellation token. .NET only
FailAn expected domain-specific failure.
fromTask reports the missing-file exception as a defect (Cause.Die); attemptTask
places it in the typed error channel (Cause.Fail), where the caller is expected to handle it.
The Task and Async interop guide covers cancellation and all supported carriers.
Related guides
- Flow construction reference lists every constructor and conversion.
- Defects explains when an exception should remain a defect and when an attempt constructor is appropriate.

