// Functions passed to C (ADR 0015). import std/io type Compare = fn(Int, Int) -> Int / // Accepted: total or `` callbacks, also through an alias. @extern c "ok_sort" fn sort :: xs: List compare: fn(Int, Int) -> Int / -> List / @extern c "ok_apply" fn apply :: f: fn(Int) -> Int x: Int -> Int @extern c "ok_alias" fn sort_with :: xs: List compare: Compare -> List / // A callback runs inside the C call: it cannot wait for anything. @extern c "bad_io" fn each :: xs: List f: fn(Int) -> () / //~ ERROR E0113 may perform `` at most -> () / @extern c "bad_open" fn each_open :: xs: List f: fn(Int) -> () / //~ ERROR E0113 may perform `` at most -> () / // A blocking call runs on another thread. @extern c "bad_blocking" blocking fn slow_sort :: xs: List compare: fn(Int, Int) -> Int //~ ERROR E0113 cannot take a function -> List / // The extern performs what its callback performs. @extern c "bad_row" fn sort_total :: xs: List compare: fn(Int, Int) -> Int / //~ ERROR E0113 performs `ffi` too -> List // Functions reach C only as parameters. @extern c "bad_nested" fn first :: fs: List Int> //~ ERROR E0113 only a parameter itself -> Int pub fn main :: -> () / { io.println (show (sort [2, 1] fn a b { a - b })) io.println (show (apply fn x { x + 1 } 1)) io.println (show (sort_with [2, 1] fn a b { a - b })) }