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.
Dernier runRéussi#121 · main · 16 min 23 s
0Étoiles
0Forks
40Issues
| 1 | // Early `return` (ADR 0037): what it returns, and where it can't be. |
| 2 | |
| 3 | fn checked :: n: Int -> Int |
| 4 | { |
| 5 | if n < 0 |
| 6 | { |
| 7 | return "negative" //~ ERROR E0202 expected `Int`, found `String` |
| 8 | } |
| 9 | n |
| 10 | } |
| 11 | |
| 12 | fn unit_only :: n: Int -> Int |
| 13 | { |
| 14 | if n < 0 |
| 15 | { |
| 16 | return //~ ERROR E0202 expected `Int`, found `()` |
| 17 | } |
| 18 | n |
| 19 | } |
| 20 | |
| 21 | /// `return` has type `Never`: it fits where any value is expected. |
| 22 | fn fits :: n: Int -> String |
| 23 | { |
| 24 | let label: String = if n > 0 { "positive" } else { return "other" } |
| 25 | label |
| 26 | } |
| 27 | |
| 28 | fn in_a_lambda :: xs: List<Int> -> List<Int> |
| 29 | { |
| 30 | list.map |
| 31 | xs |
| 32 | fn x |
| 33 | { |
| 34 | if x < 0 |
| 35 | { |
| 36 | return 0 //~ ERROR E0119 from a lambda |
| 37 | } |
| 38 | x |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn in_a_capture :: n: Int -> fn(Int) -> Int |
| 43 | { |
| 44 | add _ (return 1) //~ ERROR E0119 from a capture |
| 45 | } |
| 46 | |
| 47 | fn add :: a: Int b: Int -> Int |
| 48 | { |
| 49 | a + b |
| 50 | } |
| 51 | |
| 52 | effect ask |
| 53 | { |
| 54 | fn ask :: -> Int |
| 55 | } |
| 56 | |
| 57 | fn in_a_clause :: -> Int |
| 58 | { |
| 59 | handle |
| 60 | { |
| 61 | ask () |
| 62 | } |
| 63 | with |
| 64 | { ask -> return 1 //~ ERROR E0119 from a handler clause |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn in_a_cleanup :: |
| 69 | -> Int / <io> //~ WARN W0501 never performed |
| 70 | { |
| 71 | defer |
| 72 | { |
| 73 | return 1 //~ ERROR E0119 from a `defer` cleanup |
| 74 | } |
| 75 | 2 |
| 76 | } |
| 77 | |
| 78 | import std/list |
| 79 | |
| 80 | /// A callback that could still `return` can't outlive its `use`. |
| 81 | fn keep :: f: fn() -> a / <e> -> fn() -> a / <e> |
| 82 | { |
| 83 | f |
| 84 | } |
| 85 | |
| 86 | fn escapes :: -> Int |
| 87 | { |
| 88 | let later = |
| 89 | { |
| 90 | use <- keep //~ ERROR E0703 would escape |
| 91 | return 1 |
| 92 | } |
| 93 | 0 |
| 94 | } |
| 95 | |
| 96 | /// A callee whose callback's effects are fixed can't let it `return`. |
| 97 | fn total :: f: fn() -> Int -> Int |
| 98 | { |
| 99 | f () |
| 100 | } |
| 101 | |
| 102 | fn fixed :: -> Int |
| 103 | { |
| 104 | use <- total |
| 105 | return 1 //~ ERROR E0502 can't leave the function from this callback |
| 106 | } |
| 107 | |
| 108 | /// Nor can an actor's body: it runs in another actor. |
| 109 | fn spawned :: -> Int / <proc> |
| 110 | { |
| 111 | let _ = process.spawn fn { return 1 } //~ ERROR E0119 from a lambda |
| 112 | { |
| 113 | use <- process.spawn |
| 114 | return 2 //~ ERROR E0502 can't leave the function from this callback |
| 115 | } |
| 116 | 0 |
| 117 | } |
| 118 | |
| 119 | import std/process.{type proc} |