tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

175 lignes · 3755 octetsBrut · Historique
1// What a closure captured is checked when it is sent (ADR 0004, Revision
2// 6): function types carry a capture row, `send fn` is a function that can
3// be sent, and a plain `fn` field or result may hold any function.
4
5import std/ffi.{type Resource}
6import std/process.{type Subject, type proc}
7import std/ref.{type Ref}
8
9effect note
10{
11 fn note :: text: String -> ()
12}
13
14type Holder { Holder(callback: fn() -> Int) }
15
16type Task { Task(run: send fn() -> Int) }
17
18type Handle { Handle }
19
20// A spawned function is sent: it may capture only sendable values.
21fn spawns_fine :: n: Int reply: Subject<Int> -> () / <proc>
22{
23 let _ = process.spawn fn { process.send reply (n + 1) }
24 ()
25}
26
27fn spawns_a_ref :: counter: Ref<h, Int> -> () / <proc>
28{
29 let _ =
30 process.spawn
31 fn //~ ERROR E0704 `Ref<h, Int>` cannot be sent
32 {
33 let _ = counter
34 ()
35 }
36 ()
37}
38
39// A pinned resource is no better: a spawned closure may not capture one.
40fn spawns_a_resource :: h: Resource<Handle> -> () / <proc>
41{
42 let _ =
43 process.spawn
44 fn //~ ERROR E0704 `Resource<Handle>` cannot be sent
45 {
46 let _ = h
47 ()
48 }
49 ()
50}
51
52// A closure using a handler of this actor cannot leave it.
53fn sends_a_handler :: -> () / <proc>
54{
55 handle
56 {
57 let mine = process.subject ()
58 process.send
59 mine
60 fn { note "hi" } //~ ERROR E0704 this function cannot be sent
61 }
62 with
63 { note _ -> () }
64}
65
66// Generic captures need the bound, written or inferred.
67fn spawns_generic :: x: a reply: Subject<Int> -> () / <proc>
68{
69 let _ =
70 process.spawn
71 fn //~ ERROR E0402 `a` is not known to implement `Send`
72 {
73 let _ = x
74 process.send reply 0
75 }
76 ()
77}
78
79fn spawns_generic_bounded :: x: a reply: Subject<a> -> () / <proc> where Send<a>
80{
81 let _ = process.spawn fn { process.send reply x }
82 ()
83}
84
85fn spawns_inferred :: x reply
86{
87 process.spawn fn { process.send reply x }
88}
89//~ TYPE spawns_inferred: fn(a, Subject<a>) -> Pid / <proc> where Send<a>
90
91// A closure sent as a message.
92fn sends_closures ::
93 to: Subject<send fn() -> Int>
94 n: Int
95 r: Ref<h, Int>
96 -> () / <proc>
97{
98 process.send to fn { n * 2 }
99 process.send
100 to
101 fn //~ ERROR E0704 `Ref<h, Int>` cannot be sent
102 {
103 let _ = r
104 n
105 }
106}
107
108// A sent closure may not capture a pinned resource either.
109fn sends_a_resource ::
110 to: Subject<send fn() -> Int>
111 h: Resource<Handle>
112 -> () / <proc>
113{
114 process.send
115 to
116 fn //~ ERROR E0704 `Resource<Handle>` cannot be sent
117 {
118 let _ = h
119 1
120 }
121}
122
123// A plain `fn` parameter may be given any function: it cannot be sent.
124fn forwards :: f: fn() -> Int to: Subject<send fn() -> Int> -> () / <proc>
125{
126 process.send to f //~ ERROR E0202 type mismatch
127}
128
129fn forwards_sendable ::
130 f: send fn() -> Int
131 to: Subject<send fn() -> Int>
132 -> () / <proc>
133{
134 process.send to f
135}
136
137// A plain `fn` field holds any function, so its type is never `Send`.
138fn holders :: counter: Ref<h, Int> to: Subject<Holder> -> () / <proc>
139{
140 let _ =
141 Holder( fn
142 {
143 let _ = counter
144 1
145 }
146 )
147 process.send to Holder(fn { 1 }) //~ ERROR E0704 `Holder` cannot be sent
148}
149
150fn tasks :: n: Int to: Subject<Task> -> () / <proc>
151{
152 process.send to Task(fn { n })
153}
154
155fn task_with_a_ref :: counter: Ref<h, Int> -> Task
156{
157 Task( fn //~ ERROR E0704 `Ref<h, Int>` cannot be sent
158 {
159 let _ = counter
160 1
161 }
162 )
163}
164
165// A closure kept by this actor may capture anything, and be returned.
166fn kept :: counter: Ref<h, Int> -> fn() -> Ref<h, Int>
167{
168 fn { counter }
169}
170
171// A closure returning a checked closure is sendable as a whole.
172fn nested :: n: Int to: Subject<send fn() -> send fn() -> Int> -> () / <proc>
173{
174 process.send to fn { fn { n } }
175}