Skip to content

Commit

Permalink
draft library effects
Browse files Browse the repository at this point in the history
  • Loading branch information
magnus-madsen committed Oct 12, 2024
1 parent e9d9170 commit c0db3c2
Showing 1 changed file with 67 additions and 7 deletions.
74 changes: 67 additions & 7 deletions src/library-effects.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,76 @@
## Library Effects

TODO: Clock
Flix comes with a collection of built-in effects that are available in the
standard library.

TODO: Console
Each effect introduces a companion module which contains both `handle` and `run`
functions.

TODO: Exec
Here are some of them:

TODO: Logger
### Clock

TODO: Process
```flix
eff Clock {
/// Returns a measure of time since the epoch in the given time unit `u`.
pub def currentTime(u: TimeUnit): Int64
}
```

TODO: Random
The `Clock` module also contains the methods:

TODO: Time
```flix
TBD
```

Every effect comes with these `handle` and `run` functions in the companion module of the effect.

### Console

### Exec

### Logger

### Process

### Random

### Time

### Running Effects

If we have a program that uses the `Clock` effect:

```flix
def getEpoch(): Int64 \ {Clock} = Clock.now()
```

We can run it by writing:

```flix
def main(): Unit \ IO =
println(Clock.handle(getEpoch)())
```

If a function has multiple effects:

```flix
def greet(name: String): Unit \ {Clock, Console} = ...
```

We can run the program by writing:

```flix
def main(): Unit \ IO =
println(Clock.handle(Console.handle(getEpoch))())
```

### Using App

Manually adding all the handlers can be tedius. For a simpler solution, which
handles all library provided effects, we can use `App.run`:

```flix
def main(): Unit \ IO =
App.run(myFunction)
```

0 comments on commit c0db3c2

Please sign in to comment.