// Messages that the sender keeps using: moved, copied or shared by the // native runtime (ADR 0002, Revision 1), invisible to the program. import std/io import std/list import std/process.{type Subject, type proc, Crashed, Down} import std/ref effect note { fn note :: text: String -> () } /// What `@extern` code can do: no type crosses it, so the compiler cannot /// check what is sent through it. The runtime still refuses it (ADR 0004, /// Revision 6: the backstop). @extern c "tl_coerce" fn unchecked :: x: a -> b type Job { Sum(List, Subject), Both(List, List, Subject) } fn worker :: jobs: Subject> -> () / { let inbox = process.subject () process.send jobs inbox serve inbox } fn serve :: inbox: Subject -> () / { match process.receive_forever inbox { Sum(xs, reply) -> process.send reply (list.sum xs) , Both(xs, ys, reply) -> process.send reply (list.sum xs + list.sum ys) } serve inbox } pub fn main :: -> () / { let jobs = process.subject () let _ = process.spawn fn { worker jobs } let inbox = process.receive_forever jobs let reply = process.subject () // Small, kept by the sender: copied. let small = [1, 2, 3] process.send inbox Sum(small, reply) io.println ( "small " <> show (process.receive_forever reply) <> " " <> show (list.sum small) ) // Large, kept by the sender: shared. let large = list.range 1 1000 process.send inbox Sum(large, reply) io.println ( "large " <> show (process.receive_forever reply) <> " " <> show (list.length large) ) // The same list twice in one message, and again in a later one. process.send inbox Both(small, small, reply) io.println ("twice " <> show (process.receive_forever reply)) process.send inbox Both(large, small, reply) io.println ("mixed " <> show (process.receive_forever reply)) // Not kept: moved. process.send inbox Sum(list.range 1 10, reply) io.println ("moved " <> show (process.receive_forever reply)) // A spawned actor's closure is sent too. let done = process.subject () let _ = process.spawn fn { process.send done (list.sum large + list.sum small) } io.println ("captured " <> show (process.receive_forever done)) // A closure capturing a mutable reference cannot leave its actor: the // compiler refuses to send one (tests/ui/send_closures.lse), and when // `@extern` code hides it, the sender crashes. let (_, down) = { use <- process.spawn_monitor region { let counter = ref.new 1 let mine = process.subject () process.send mine (unchecked fn { ref.get counter }) } } let Down(_, reason) = process.receive_forever down io.println (show reason) // Nor can a closure using a handler of this actor. let (_, down) = { use <- process.spawn_monitor handle { let mine = process.subject () process.send mine (unchecked fn { note "hello" }) } with { note text -> () } } let Down(_, reason) = process.receive_forever down io.println (show reason) } //> small 6 6 //> large 500500 1000 //> twice 12 //> mixed 500506 //> moved 55 //> captured 500506 //> Crashed(cannot send a region or a handler) //> Crashed(cannot send a region or a handler)