Skuld.Syntax (skuld v0.23.0)
View SourceUnified syntax module providing do-notation macros for Skuld computations.
Usage
use Skuld.Syntax
# Now you have access to:
# - comp (inline computation block)
# - defcomp, defcompp (function definitions)
# - query (inline query block with automatic batching)
# - defquery, defqueryp (query function definitions)Example
use Skuld.Syntax
alias Skuld.Effects.State
comp do
x <- State.get()
y = x + 1
_ <- State.put(y)
return(y)
endDefining Functions
defcomp increment() do
x <- State.get()
_ <- State.put(x + 1)
return(x + 1)
end
defcompp private_helper() do
ctx <- Reader.ask()
return(ctx.value)
endQuery Functions
defquery user_with_orders(id) do
user <- Users.get_user(id)
orders <- Orders.get_by_user(user.id)
{user, orders}
end
defqueryp private_fetch(id) do
data <- DataSource.fetch(id)
data
endSyntax Reference
x <- effect()- bind the result of an effectful computationx = expr- pure variable binding (unchanged)return(value)- lift a pure value into a computation (optional - see auto-lifting)- Last expression is auto-lifted if not already a computation
Auto-Lifting
Non-computation values are automatically wrapped in pure(). This means:
- Final expressions don't need
return():x + 1works as final line ifwithoutelseworks:_ <- if cond, do: effect()(nil auto-lifted)- Any plain value in a bind position is treated as
pure(value)
See Also
Skuld.Comp.CompBlock- macro implementation detailsSkuld.Comp- core computation primitives