Skip to content

Commit 28cd17f

Browse files
authored
add examples for static and sync service implementations, closes #1256 (#1257)
1 parent 85af6a3 commit 28cd17f

File tree

1 file changed

+24
-2
lines changed
  • content/src/content/docs/docs/requirements-management

1 file changed

+24
-2
lines changed

content/src/content/docs/docs/requirements-management/layers.mdx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,13 +1101,35 @@ The `Effect.Service` API supports multiple ways to define a service:
11011101

11021102
| Method | Description |
11031103
| --------- | -------------------------------------------------- |
1104-
| `effect` | Defines a service with an effectful constructor. |
1105-
| `sync` | Defines a service using a synchronous constructor. |
11061104
| `succeed` | Provides a static implementation of the service. |
1105+
| `sync` | Defines a service using a synchronous constructor. |
1106+
| `effect` | Defines a service with an effectful constructor. |
11071107
| `scoped` | Creates a service with lifecycle management. |
11081108

11091109
**Example** (Defining a Service with a Static Implementation)
11101110

1111+
This is the simplest way to define a service. It is useful when you want to provide a constant value for the service.
1112+
1113+
```ts twoslash
1114+
import { Effect } from "effect"
1115+
1116+
class MagicNumber extends Effect.Service<MagicNumber>()("MagicNumber", {
1117+
succeed: { value: 42 }
1118+
}) {}
1119+
1120+
// ┌─── Effect<void, never, MagicNumber>
1121+
//
1122+
const program = Effect.gen(function* () {
1123+
const magicNumber = yield* MagicNumber
1124+
console.log(`The magic number is ${magicNumber.value}`)
1125+
})
1126+
1127+
Effect.runPromise(program.pipe(Effect.provide(MagicNumber.Default)))
1128+
// The magic number is 42
1129+
```
1130+
1131+
**Example** (Defining a Service with a Synchronous Constructor)
1132+
11111133
```ts twoslash
11121134
import { Effect, Random } from "effect"
11131135

0 commit comments

Comments
 (0)