tobogganSe connecter

louise

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

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