F# Effect System based on SRTP.
Caution: Multiple handlers cannot be composed.
SharpLab compilation result of example.fsx
- Available on Nuget:
dotnet add package EffFs
And more examples
#load "../src/EffFs/EffFs.fs"
open EffFs
type RandomInt = RandomInt of int with
static member Effect(_) = Eff.marker<int>
type Logging = Logging of string with
static member Effect(_) = Eff.marker<unit>
let inline foo() = eff {
let! a = RandomInt 100
do! Logging (sprintf "%d" a)
let b = a + a
return (a, b)
}
let rand = System.Random()
type Handler = Handler with
static member inline Value(_, x) = x
static member inline Handle(RandomInt a, k) =
rand.Next(a) |> k
static member inline Handle(Logging s, k) =
printfn "[Log] %s" s; k()
foo()
|> Eff.handle Handler
|> printfn "%A"
// example output
(*
[Log] 66
(66, 132)
*)
.NET 5.0 or later is required.
$ dotnet build src/EffFs # Debug
$ dotnet build src/EffFs -c Release
$ dotnet fsi --exec example/example.fs
$ # edit RELEASE_NOTES.md
$ dotnet fake build -t assemblyinfo
$ dotnet fake build -t pack