@@ -11,7 +11,7 @@ $ luarocks --local install eff
11
11
```
12
12
13
13
# usage
14
- ` eff ` provides four objects, ` inst ` , ` perform ` , ` handler ` and ` handlers ` .
14
+ ` eff ` provides four objects, ` inst ` , ` perform ` and ` handler ` .
15
15
16
16
## effect instantiation and invocation
17
17
` inst ` generates an effect instance.
@@ -23,18 +23,23 @@ perform(Write("Hello!")) -- invocation
23
23
```
24
24
25
25
## effect handler
26
- ` handler(eff, value-handler, effect-handler ) `
26
+ ` handler(h ) `
27
27
28
- ` handler ` requires the handling effect ` eff ` , ` value-handler ` and ` effect-handler ` , and returns a _ handling function_ .
28
+ ` handler ` requires a table and returns a _ handling function_ .
29
+ The table has fields, a value handler as ` val ` and ` [Eff] ` as an effect handler corresponding to ` Eff ` .
29
30
"Handling an expression with a handler" is translated into an application passing a thunk, containing the expression, to the _ handling function_ .
30
31
31
32
``` lua
32
- local printh = handler (Write ,
33
- function (v ) print (" printh ended" , v ) end ,
34
- function (k , arg )
35
- print (arg )
36
- k ()
37
- end )
33
+ local printh = handler {
34
+ val = function (v ) print (" printh ended" , v ) end ,
35
+ [Write ] = function (k , arg )
36
+ print (arg )
37
+ k ()
38
+ end ,
39
+ [Read ] = function (k )
40
+ return k (" baa" )
41
+ end
42
+ }
38
43
39
44
printh (function ()
40
45
local x = perform (Write (" hello" ))
@@ -47,37 +52,18 @@ printh ended nil
47
52
]]
48
53
```
49
54
50
- ` handlers ` can handle multiple effects.
51
- Pass ` {eff, effect-handler} ` tuples to the function
52
- ``` lua
53
- local someh = handlers (
54
- function (v ) return v end , -- value handler
55
- {Foo , function (k , v ) print (" catch Foo" ) return k (v ) end },
56
- {Bar , function (k , v ) print (" catch Bar" ) return k (v ) end }
57
- )
58
- ```
59
- or you can write such as
60
- ``` lua
61
- local awesomeh = handlers {
62
- function (v ) return v end , -- value handler
63
- -- this is not `[[Foo]]`, just [Foo]. Not a macro, but a standard table syntax.
64
- [Foo ] = function (k , v ) print (" catch foo" ) return k (v ) end ,
65
- [Bar ] = function (k , v ) print (" catch bar" ) return k (v ) end ,
66
- }
67
- ```
68
-
69
- ### limitation about continuation
55
+ ### restriction about continuation
70
56
The continuation ` effect handler ` received is * ONE-SHOT* , in other words, the continuatoin * cannot* run more than twice.
71
57
72
58
``` lua
73
- handler (Write ,
74
- function (v ) print (" printh ended" , v ) end ,
75
- function (k , arg )
76
- print (arg )
77
- k ()
78
- k () -- call continuation twice
79
- end )
80
- (function ()
59
+ handler ({
60
+ val = function (v ) print (" printh ended" , v ) end ,
61
+ [ Write ] = function (k , arg )
62
+ print (arg )
63
+ k ()
64
+ k () -- call continuation twice
65
+ end
66
+ }) (function ()
81
67
perform (Write (" Foo" ))
82
68
end )
83
69
0 commit comments