
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 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 | |
| 5 | import std/dist/remote.{type Remote} |
| 6 | import std/ffi.{type Resource} |
| 7 | import std/io |
| 8 | import std/list |
| 9 | import std/process.{type Dynamic, type Pid, type Subject, type proc} |
| 10 | import std/time.{type clock} |
| 11 | import std/wire.{type Arg} |
| 12 | |
| 13 | pub type Job { Job(id: Int, words: List<String>, reply: Subject<String>) } |
| 14 | |
| 15 | @thread_safe |
| 16 | type Handle { Handle } |
| 17 | |
| 18 | // Accepted: a record, a list, a subject, nothing. |
| 19 | @remote |
| 20 | fn worker :: job: Job -> () / <proc> |
| 21 | { |
| 22 | process.send job.reply (show job.id) |
| 23 | } |
| 24 | |
| 25 | @remote |
| 26 | fn sum :: numbers: List<Int> -> () / <io> |
| 27 | { |
| 28 | io.println (show numbers) |
| 29 | } |
| 30 | |
| 31 | @remote |
| 32 | fn notify :: to: Subject<Pid> -> () / <proc, clock> |
| 33 | { |
| 34 | process.send to (process.self ()) |
| 35 | let _ = time.now () |
| 36 | } |
| 37 | |
| 38 | @remote |
| 39 | fn ping :: -> () |
| 40 | { |
| 41 | () |
| 42 | } |
| 43 | |
| 44 | // A reference is a `Remote<A>`, whatever reads it. |
| 45 | fn reference :: -> Remote<Job> |
| 46 | { |
| 47 | worker |
| 48 | } |
| 49 | //~ TYPE reference: fn() -> Remote<Job> |
| 50 | |
| 51 | fn 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. |
| 58 | fn 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. |
| 64 | fn 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. |
| 73 | fn forged :: d: Dynamic -> Arg |
| 74 | { |
| 75 | wire.Arg("u", d) |
| 76 | //~^ ERROR E0117 `Arg` is opaque outside `std/wire` |
| 77 | } |
| 78 | |
| 79 | fn not_wire :: f: fn() -> () -> Arg |
| 80 | { |
| 81 | wire.arg f |
| 82 | //~^ ERROR E0706 cannot cross hosts |
| 83 | } |
| 84 | |
| 85 | // Refused: generic, ... |
| 86 | @remote |
| 87 | fn generic :: x: a -> () |
| 88 | //~^ ERROR E0707 `@remote` function `generic` cannot be generic |
| 89 | { |
| 90 | () |
| 91 | } |
| 92 | |
| 93 | @remote |
| 94 | fn polymorphic :: x: Int -> () / <e> |
| 95 | //~^ ERROR E0707 cannot be generic |
| 96 | { |
| 97 | () |
| 98 | } |
| 99 | |
| 100 | // ... not one argument, a result, ... |
| 101 | @remote |
| 102 | fn two :: a: Int b: Int -> () |
| 103 | //~^ ERROR E0707 `@remote` function `two` takes 2 arguments |
| 104 | { |
| 105 | () |
| 106 | } |
| 107 | |
| 108 | @remote |
| 109 | fn answers :: x: Int -> Int |
| 110 | //~^ ERROR E0707 returns `Int`; it returns `()` |
| 111 | { |
| 112 | x |
| 113 | } |
| 114 | |
| 115 | // ... an effect nothing handles around an actor, ... |
| 116 | effect ask |
| 117 | { |
| 118 | fn ask :: -> Int |
| 119 | } |
| 120 | |
| 121 | @remote |
| 122 | fn 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 |
| 131 | fn callback :: f: fn() -> () -> () |
| 132 | //~^ ERROR E0706 a function's code |
| 133 | { |
| 134 | () |
| 135 | } |
| 136 | |
| 137 | @remote |
| 138 | fn resource :: r: Resource<Handle> -> () |
| 139 | //~^ ERROR E0706 a `Resource` is a handle to foreign state |
| 140 | { |
| 141 | () |
| 142 | } |
| 143 | |
| 144 | @remote |
| 145 | fn 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 |
| 153 | fn 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" |
| 162 | fn 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 |
| 169 | type Message |
| 170 | { Message } |
| 171 | |
| 172 | trait Run<a> |
| 173 | { |
| 174 | @remote |
| 175 | //~^ ERROR E0114 `@remote` does not apply to a method |
| 176 | fn run :: |
| 177 | x: a |
| 178 | -> () |
| 179 | } |