<Services, S>(self: Context<Services>, service: Reference<S>): SGets 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"export const const getReferenceUnsafe: <Services, S>(
self: Context<Services>,
service: Reference<S>
) => S
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"
getReferenceUnsafe = <function (type parameter) Services in <Services, S>(self: Context<Services>, service: Reference<S>): SServices, function (type parameter) S in <Services, S>(self: Context<Services>, service: Reference<S>): SS>(self: Context<Services>(parameter) self: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self: interface Context<in Services>Immutable collection of service implementations used for dependency
injection in Effect programs.
Details
The type parameter tracks the service identifiers available in the context.
At runtime, services are stored by each key's string key.
Example (Creating a context with multiple services)
import { Context } from "effect"
// Create a context with multiple services
const Logger = Context.Service<{ log: (msg: string) => void }>("Logger")
const Database = Context.Service<{ query: (sql: string) => string }>(
"Database"
)
const context = Context.make(Logger, {
log: (msg: string) => console.log(msg)
})
.pipe(Context.add(Database, { query: (sql) => `Result: ${sql}` }))
Context<function (type parameter) Services in <Services, S>(self: Context<Services>, service: Reference<S>): SServices>, service: Reference<S>(parameter) service: {
defaultValue: () => Shape;
of: (this: void, self: S) => S;
context: (self: S) => Context<never>;
use: (f: (service: S) => Effect<A, E, R>) => Effect<A, E, R>;
useSync: (f: (service: S) => A) => Effect<A, never, never>;
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service: interface Reference<in out Shape>Service key with a lazily computed default value.
Details
When a Reference is requested from a Context that does not contain an
override, Context getters that resolve references return the cached default
value instead of failing.
Example (Defining a reference with a default value)
import { Context } from "effect"
// Define a reference with a default value
const LoggerRef: Context.Reference<{ log: (msg: string) => void }> =
Context.Reference("Logger", {
defaultValue: () => ({ log: (msg: string) => console.log(msg) })
})
// The reference can be used without explicit provision
const context = Context.empty()
const logger = Context.get(context, LoggerRef) // Uses default value
Creates a context key with a default value.
When to use
Use when you need to define a context key with a lazily computed default
value.
Details
Context.Reference allows you to create a key that can hold a value. You
can provide a default value for the service, which will automatically be used
when the context is accessed, or override it with a custom implementation
when needed. The default value is computed lazily and cached on the
reference.
Example (Creating references with default values)
import { Context } from "effect"
// Create a reference with a default value
const LoggerRef = Context.Reference("Logger", {
defaultValue: () => ({ log: (msg: string) => console.log(msg) })
})
// The reference provides the default value when accessed from an empty context
const context = Context.empty()
const logger = Context.get(context, LoggerRef)
// You can also override the default value
const customContext = Context.make(LoggerRef, {
log: (msg: string) => `Custom: ${msg}`
})
const customLogger = Context.get(customContext, LoggerRef)
Reference<function (type parameter) S in <Services, S>(self: Context<Services>, service: Reference<S>): SS>): function (type parameter) S in <Services, S>(self: Context<Services>, service: Reference<S>): SS => {
if (!self: Context<Services>(parameter) self: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.Context<in Services>.mapUnsafe: ReadonlyMap<string, any>mapUnsafe.ReadonlyMap<string, any>.has(key: string): booleanhas(service: Reference<S>(parameter) service: {
defaultValue: () => Shape;
of: (this: void, self: S) => S;
context: (self: S) => Context<never>;
use: (f: (service: S) => Effect<A, E, R>) => Effect<A, E, R>;
useSync: (f: (service: S) => A) => Effect<A, never, never>;
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service.Key<out Identifier, out Shape>.key: stringkey)) {
return const getDefaultValue: (
ref: Reference<any>
) => any
getDefaultValue(service: Reference<S>(parameter) service: {
defaultValue: () => Shape;
of: (this: void, self: S) => S;
context: (self: S) => Context<never>;
use: (f: (service: S) => Effect<A, E, R>) => Effect<A, E, R>;
useSync: (f: (service: S) => A) => Effect<A, never, never>;
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service as any)
}
return self: Context<Services>(parameter) self: {
mapUnsafe: ReadonlyMap<string, any>;
mutable: boolean;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.Context<in Services>.mapUnsafe: ReadonlyMap<string, any>mapUnsafe.ReadonlyMap<string, any>.get(key: string): anyget(service: Reference<S>(parameter) service: {
defaultValue: () => Shape;
of: (this: void, self: S) => S;
context: (self: S) => Context<never>;
use: (f: (service: S) => Effect<A, E, R>) => Effect<A, E, R>;
useSync: (f: (service: S) => A) => Effect<A, never, never>;
Identifier: Identifier;
Service: Shape;
key: string;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
service.Key<out Identifier, out Shape>.key: stringkey)! as any
}