After this commit, we have to use an explicit `discard` in code such as ``` def g (x : Nat) : IO Nat := ... def f (x : Nat) : IO Unit := do discard <| g x -- type error without the `discard` IO.println x ``` Motivation: prevent users from making mistakes such as ``` def f (xs : Array Nat) : IO Unit := do xs.set! 0 1 IO.println xs ``` when they meant to write ``` def f (xs : Array Nat) : IO Unit := do let xs := xs.set! 0 1 IO.println xs ```
49 lines
1.5 KiB
Text
49 lines
1.5 KiB
Text
--
|
||
open IO.Process
|
||
|
||
def usingIO {α} (x : IO α) : IO α := x
|
||
|
||
#eval usingIO do
|
||
let child ← spawn { cmd := "sh", args := #["-c", "exit 1"] };
|
||
child.wait
|
||
|
||
#eval usingIO do
|
||
let child ← spawn { cmd := "sh", args := #["-c", "echo hi!"] };
|
||
child.wait
|
||
|
||
#eval usingIO do
|
||
let child ← spawn { cmd := "sh", args := #["-c", "echo ho!"], stdout := Stdio.piped };
|
||
discard $ child.wait;
|
||
child.stdout.readToEnd
|
||
|
||
#eval usingIO do
|
||
let child ← spawn { cmd := "head", args := #["-n1"], stdin := Stdio.piped, stdout := Stdio.piped };
|
||
child.stdin.putStrLn "hu!";
|
||
child.stdin.flush;
|
||
discard $ child.wait;
|
||
child.stdout.readToEnd
|
||
|
||
#eval usingIO do
|
||
let child ← spawn { cmd := "true", stdin := Stdio.piped };
|
||
discard $ child.wait;
|
||
child.stdin.putStrLn "ha!";
|
||
child.stdin.flush <|> IO.println "flush of broken pipe failed"
|
||
|
||
#eval usingIO do
|
||
-- produce enough output to fill both pipes on all platforms
|
||
let out ← output { cmd := "sh", args := #["-c", "printf '%100000s' >& 2; printf '%100001s'"] };
|
||
IO.println out.stdout.length;
|
||
IO.println out.stderr.length
|
||
|
||
#eval usingIO do
|
||
-- With a non-empty stdin, cat would wait on input forever
|
||
let child ← spawn { cmd := "sh", args := #["-c", "cat"], stdin := Stdio.null };
|
||
child.wait
|
||
|
||
#eval usingIO do
|
||
let child ← spawn { cmd := "sh", args := #["-c", "echo nullStdout"], stdout := Stdio.null };
|
||
child.wait
|
||
|
||
#eval usingIO do
|
||
let child ← spawn { cmd := "sh", args := #["-c", "echo nullStderr >& 2"], stderr := Stdio.null };
|
||
child.wait
|