You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: c-ffi/calling-c.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -106,10 +106,10 @@ FFI calls to functions that __might__ raise an error __must__ mark it as such by
106
106
@os_send[U64](_event, data.cstring(), data.size()) ? // May raise an error
107
107
```
108
108
109
-
If a signature declaration is used then that must be marked as possibly raising an error in the same way. The FFI call site then does not have to mark it as well, although doing so is allowed.
109
+
If a signature declaration is used then that must be marked as possibly raising an error in the same way. The FFI call site must mark it as well.
110
110
111
111
```pony
112
112
use @os_send[U64](ev: Event, buf: Pointer[U8] tag, len: U64) ?
113
113
114
-
@os_send(_event, data.cstring(), data.size()) // May raise an error
114
+
@os_send(_event, data.cstring(), data.size())? // May raise an error
Copy file name to clipboardExpand all lines: capabilities/recovering-capabilities.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,29 +22,29 @@ Here's a more complicated example from the standard library:
22
22
23
23
```pony
24
24
recover
25
-
var s = String((prec' + 1).max(width.max(31)))
25
+
var s = String((prec + 1).max(width.max(31)))
26
26
var value = x
27
27
28
28
try
29
29
if value == 0 then
30
-
s.push(table(0))
30
+
s.push(table(0)?)
31
31
else
32
32
while value != 0 do
33
-
let index = (value = value / base) - (value * base)
34
-
s.push(table(index.usize()))
33
+
let index = ((value = value / base) - (value * base))
34
+
s.push(table(index.usize())?)
35
35
end
36
36
end
37
37
end
38
38
39
-
s.append(typestring)
40
39
_extend_digits(s, prec')
40
+
s.append(typestring)
41
41
s.append(prestring)
42
42
_pad(s, width, align, fill)
43
43
s
44
44
end
45
45
```
46
46
47
-
That's from `ToString`. It creates a `String ref`, does a bunch of stuff with it, and finally returns it as a `String iso`.
47
+
That's from `format/_FormatInt`. It creates a `String ref`, does a bunch of stuff with it, and finally returns it as a `String iso`.
48
48
49
49
Both of those examples use the default reference capability for a `recover` expression, since they don't specify one. The default for any mutable reference capability is `iso` and the default for any immutable reference capability is `val`. You can also give an explicit one:
Copy file name to clipboardExpand all lines: expressions/exceptions.md
+11-6Lines changed: 11 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,9 +16,9 @@ else
16
16
end
17
17
```
18
18
19
-
In the above code callA() will always be executed and so will callB(). If the result of callB() is true then we will proceed to callC() in the normal fashion and callD() will not then be executed.
19
+
In the above code `callA()` will always be executed and so will `callB()`. If the result of `callB()` is true then we will proceed to `callC()` in the normal fashion and `callD()` will not then be executed.
20
20
21
-
However, if callB() returns false, then an error will be raised. At this point, execution will stop and the nearest enclosing error handler will be found and executed. In this example that is, our else block and so callD() will be executed.
21
+
However, if `callB()` returns false, then an error will be raised. At this point, execution will stop and the nearest enclosing error handler will be found and executed. In this example that is, our else block and so `callD()` will be executed.
22
22
23
23
In either case, execution will then carry on with whatever code comes after the try `end`.
24
24
@@ -28,15 +28,18 @@ If you want to do something that might raise an error, but you don't care if it
28
28
29
29
```pony
30
30
try
31
-
call() // May raise an error
31
+
// Do something that may raise an error
32
32
end
33
33
```
34
34
35
35
__Is there anything my error handler has to do?__ No. If you provide an error handler then it must contain some code, but it is entirely up to you what it does.
36
36
37
+
__What's the resulting value of a try block?__ The result of a try block is the value of the last statement in the `try` block, or the the value of the last statement in the `else` clause if an error was raised. If an error was raised and there was no `else` clause provided, the result value will be `None`.
38
+
39
+
37
40
## Partial functions
38
41
39
-
Pony does not require that all errors are handled immediately as in our previous examples. Instead, functions can raise errors that are handled by whatever code calls them. These are called partial functions (this is a mathematical term meaning a function that does not have a defined result for all possible inputs, i.e. arguments). Partial functions __must__ be marked as such in Pony with a `?`.
42
+
Pony does not require that all errors are handled immediately as in our previous examples. Instead, functions can raise errors that are handled by whatever code calls them. These are called partial functions (this is a mathematical term meaning a function that does not have a defined result for all possible inputs, i.e. arguments). Partial functions __must__ be marked as such in Pony with a `?`, both in the function signature (after the return type) and at the call site (after the closing parentheses).
40
43
41
44
For example, a somewhat contrived version of the factorial function that accepts a signed integer will error if given a negative input. It's only partially defined over its valid input type.
42
45
@@ -46,12 +49,14 @@ fun factorial(x: I32): I32 ? =>
46
49
if x == 0 then
47
50
1
48
51
else
49
-
x * factorial(x - 1)
52
+
x * factorial(x - 1)?
50
53
end
51
54
```
52
55
53
56
Everywhere that an error can be generated in Pony (an error command, a call to a partial function, or certain built-in language constructs) must appear within a try block or a function that is marked as partial. This is checked at compile time, ensuring that an error cannot escape handling and crash the program.
54
57
58
+
Prior to Pony 0.16.0, call sites of partial functions were not required to be marked with a '?'. This often led to confusion about the possibilities for control flow when reading code. Having every partial function call site clearly marked makes it very easy for the reader to immediately understand everywhere that a block of code may jump away to the nearest error handler, making the possible control flow paths more obvious and explicit.
59
+
55
60
## Partial constructors and behaviours
56
61
57
62
Constructors may also be marked as partial. If a constructor raises an error then the construction is considered to have failed and the object under construction is discarded without ever being returned to the caller.
@@ -124,7 +129,7 @@ The value of a `with` expression is the value of the last expression in the bloc
124
129
125
130
## Language constructs that can raise errors
126
131
127
-
The only language construct that can raise an error, other than the error command or calling a partial method, is the `as` command. This converts the given value to the specified type if it can be. If it can't then an error is raised. This means that the `as` command can only be used inside a try block or a partial method.
132
+
The only language construct that can raise an error, other than the `error` command or calling a partial method, is the `as` command. This converts the given value to the specified type if it can be. If it can't then an error is raised. This means that the `as` command can only be used inside a try block or a partial method.
0 commit comments