tobogganSe connecter

louise

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

190 lignes · 3784 octetsBrut · Historique
1// `@remote` functions (ADR 0072): entry points another node spawns, by
2// key, with one argument that crosses hosts. The name used as a value is
3// a `Remote<A>`; called, it is a function.
4
5import std/dist/remote.{type Remote}
6import std/ffi.{type Resource}
7import std/file.{type fs}
8import std/io
9import std/list
10import std/process.{type Dynamic, type Pid, type Subject, type proc}
11import std/time.{type clock}
12import std/wire.{type Arg}
13
14pub type Job { Job(id: Int, words: List<String>, reply: Subject<String>) }
15
16@thread_safe
17type Handle { Handle }
18
19// Accepted: a record, a list, a subject, nothing.
20@remote
21fn worker :: job: Job -> () / <proc>
22{
23 process.send job.reply (show job.id)
24}
25
26@remote
27fn sum :: numbers: List<Int> -> () / <io>
28{
29 io.println (show numbers)
30}
31
32@remote
33fn notify :: to: Subject<Pid> -> () / <proc, clock>
34{
35 process.send to (process.self ())
36 let _ = time.now ()
37}
38
39@remote
40fn ping :: -> ()
41{
42 ()
43}
44
45// A reference is a `Remote<A>`, whatever reads it.
46fn reference :: -> Remote<Job>
47{
48 worker
49}
50//~ TYPE reference: fn() -> Remote<Job>
51
52fn inferred ::
53{
54 (sum, notify, ping)
55}
56//~ TYPE inferred: fn() -> (Remote<List<Int>>, Remote<Subject<Pid>>, Remote<()>)
57
58// Called, it is a function like any other.
59fn called :: reply: Subject<String> -> () / <proc>
60{
61 worker Job(1, [], reply)
62}
63
64// A reference is not a closure: wrap it in a lambda to pass one.
65fn as_closure :: jobs: List<Job> -> () / <proc>
66{
67 list.each jobs worker
68 //~^ ERROR E0202 `worker` is `@remote`: its name, not called, is a reference
69 list.each jobs fn job { worker job }
70}
71
72// The dispatcher's argument pairs a value with its own type's
73// descriptor: a program cannot pair it with another.
74fn forged :: d: Dynamic -> Arg
75{
76 wire.Arg("u", d)
77 //~^ ERROR E0117 `Arg` is opaque outside `std/wire`
78}
79
80fn not_wire :: f: fn() -> () -> Arg
81{
82 wire.arg f
83 //~^ ERROR E0706 cannot cross hosts
84}
85
86// Refused: generic, ...
87@remote
88fn generic :: x: a -> ()
89//~^ ERROR E0707 `@remote` function `generic` cannot be generic
90{
91 ()
92}
93
94@remote
95fn polymorphic :: x: Int -> () / <e>
96//~^ ERROR E0707 cannot be generic
97{
98 ()
99}
100
101// ... not one argument, a result, ...
102@remote
103fn two :: a: Int b: Int -> ()
104//~^ ERROR E0707 `@remote` function `two` takes 2 arguments
105{
106 ()
107}
108
109@remote
110fn answers :: x: Int -> Int
111//~^ ERROR E0707 returns `Int`; it returns `()`
112{
113 x
114}
115
116// ... an effect nothing handles around an actor, ...
117effect ask
118{
119 fn ask :: -> Int
120}
121
122@remote
123fn asks :: x: Int -> () / <ask>
124//~^ ERROR E0707 performs `ask`, which nothing handles
125{
126 let _ = ask ()
127 ()
128}
129
130// Local actors receive the fs root; a remotely invocable host entry does
131// not (ADR 0084).
132@remote
133fn reads_the_host :: path: String -> () / <fs>
134//~^ ERROR E0707 performs `fs`, which is denied to `@remote` functions
135{
136 let _ = file.read path
137 ()
138}
139
140// ... an argument that cannot cross hosts.
141@remote
142fn callback :: f: fn() -> () -> ()
143//~^ ERROR E0706 a function's code
144{
145 ()
146}
147
148@remote
149fn resource :: r: Resource<Handle> -> ()
150//~^ ERROR E0706 a `Resource` is a handle to foreign state
151{
152 ()
153}
154
155@remote
156fn dynamic :: d: Dynamic -> ()
157//~^ ERROR E0706 a `Dynamic` value's type is known only
158{
159 ()
160}
161
162// A signature is required, and a body.
163@remote
164fn unannotated :: x
165//~^ ERROR E0107 `@remote` function `unannotated` needs a type
166//~^^^ ERROR E0107 `@remote` function `unannotated` needs a return type
167{
168 ()
169}
170
171@remote
172@extern c "foreign"
173fn foreign :: x: Int -> ()
174//~^ ERROR E0114 `@remote` function `foreign` needs a body
175
176// Only on top-level functions.
177// The attribute's error is on its line.
178@remote
179//~^ ERROR E0114 `@remote` does not apply to a type
180type Message
181{ Message }
182
183trait Run<a>
184{
185 @remote
186 //~^ ERROR E0114 `@remote` does not apply to a method
187 fn run ::
188 x: a
189 -> ()
190}