-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9d9170
commit c0db3c2
Showing
1 changed file
with
67 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |