A useful program must eventally interact with the world outside the computer. Running the example above will alert the user with a greeting. A program uses the perform to create an effect.
Just as imporant is a responding to effects. Programs without effects (called pure) will always return the same answer. This next example introduces some non-determinism with the Choose effect.
Try running this example multiple times and you will randomly get heads or tails.
Many programming languages have exceptions or other mechanisms to step out from the current flow of a program. This can useful so that unexpected situations can be dealt with in a clean fashion.
This example defines an expect function that assumes a result is Ok and if it is not performs the Abort effect.
There are many different types of effects, for communicating via HTTP or two a database. Concurrenct constucts such as async/await can also be implemented with effects. Which particular effects are available depend on the environment you program runs in.
Handlers are a mechanism to intercept effects performed within a function. When testing functions it is useful to control the effects they perform. In this example, running the code will show that the inner function performs two alerts, without us having to dismiss the two alerts manually.
A program that is non-deterministic is hard to test. In this example we create an always function, that will always return the same result. We then prime it to always return true and run a function that uses the choose effect.
Now when running it multiple times we see we always get the same result.
Handlers give us one more ability when handling effectful code, resuming effectful code multiple times. In this example the function both resumes the code with both true and false and builds a list of the final responses.
With this we can see every possible answer that the code might return.
Effect are tracked by Eyg's type system this ensures that only those effects that are provided by an environment can be used by the program. For example the "Alert" effect is available in the browser but code running on Arduino would have different effects availble. Ones that represent the capabilities of the system such as taking to the input output pins of that micro controller.
Experimental implementation of shallow effect handlers. Shallow effect handlers only catch one performed effect, they need to be reapplied to catch multiple effects. This allows stateful handling to be implemented, like the example belown. However it makes them less ergonomic to use, particularly if for handlers that don't need to be stateful. The default handler, described above, is a deep handler. It will capture all performed effects.