Package: effect
Module: Context
Gets the value for a Context.Reference, returning its cached default when
the context does not contain an override.
When to use
Use when you need a Context.Reference value resolved from either a stored
override or the reference’s default value.
Details
Stored overrides take precedence. If no override is present, the reference’s default value is computed lazily and cached on the reference itself.
Gotchas
Mutable default values can be shared across contexts unless an override is
provided, because the default is cached on the Context.Reference.
Example (Getting reference defaults unsafely)
import { Context } from "effect"
const LoggerRef = Context.Reference("Logger", {
defaultValue: () => ({ log: (msg: string) => console.log(msg) })
})
const context = Context.empty()
const logger = Context.getReferenceUnsafe(context, LoggerRef)
console.log(typeof logger.log) // "function"
See
getUnsafe for unsafe access with any service keyget for type-checked reference-aware accessgetOption for optional access to non-reference keysSignature
declare const getReferenceUnsafe: <Services, S>(self: Context<Services>, service: Reference<S>) => S
Since v4.0.0