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.

40 lignes · 1093 octetsBrut · Historique
1import std/actor.{Continue, Stop}
2import std/io
3import std/process.{type Subject, type proc}
4
5type Counter { Add(Int), Get(Subject<Int>), Shutdown(Subject<String>) }
6
7fn counter_step :: total: Int message: Counter -> actor.Next<Int> / <proc, io>
8{
9 match message
10 { Add(n) -> Continue(total + n)
11 , Get(reply) ->
12 {
13 process.send reply total
14 Continue(total)
15 }
16 , Shutdown(reply) ->
17 {
18 process.send reply ("stopped at " <> show total)
19 Stop
20 }
21 }
22}
23
24pub fn main :: -> () / <io, proc>
25{
26 // A lambda: the step function declares only the effects it uses, and
27 // actors also get `clock` and `random` (ADR 0036).
28 let counter = actor.start 0 fn total message { counter_step total message }
29 actor.cast counter Add(5)
30 actor.cast counter Add(37)
31 io.println ("total " <> show (actor.call counter Get 100))
32 io.println (show (actor.try_call counter Get 100))
33 io.println (actor.call counter Shutdown 100)
34 // Stopped: nobody answers.
35 io.println (show (actor.try_call counter Get 100))
36}
37//> total 42
38//> Ok(42)
39//> stopped at 42
40//> Error(Timeout)