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.

119 lignes · 1889 octetsBrut · Historique
1// Early `return` (ADR 0037): what it returns, and where it can't be.
2
3fn checked :: n: Int -> Int
4{
5 if n < 0
6 {
7 return "negative" //~ ERROR E0202 expected `Int`, found `String`
8 }
9 n
10}
11
12fn 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.
22fn fits :: n: Int -> String
23{
24 let label: String = if n > 0 { "positive" } else { return "other" }
25 label
26}
27
28fn 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
42fn in_a_capture :: n: Int -> fn(Int) -> Int
43{
44 add _ (return 1) //~ ERROR E0119 from a capture
45}
46
47fn add :: a: Int b: Int -> Int
48{
49 a + b
50}
51
52effect ask
53{
54 fn ask :: -> Int
55}
56
57fn 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
68fn 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
78import std/list
79
80/// A callback that could still `return` can't outlive its `use`.
81fn keep :: f: fn() -> a / <e> -> fn() -> a / <e>
82{
83 f
84}
85
86fn 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`.
97fn total :: f: fn() -> Int -> Int
98{
99 f ()
100}
101
102fn 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.
109fn 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
119import std/process.{type proc}