From e04af86491d97b297406cc4cd0d77fbbfc3a94c4 Mon Sep 17 00:00:00 2001 From: JJ Date: Thu, 16 May 2024 17:40:34 -0700 Subject: docs: update website --- docs/book/ERRORS.html | 116 +++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 57 deletions(-) (limited to 'docs/book/ERRORS.html') diff --git a/docs/book/ERRORS.html b/docs/book/ERRORS.html index 1e4d27a..9ec3f6f 100644 --- a/docs/book/ERRORS.html +++ b/docs/book/ERRORS.html @@ -7,7 +7,7 @@ - + @@ -174,66 +174,84 @@

Error Handling

-

Puck's error handling is shamelessly stolen from Swift. It uses a combination of Option/Result types and try/catch statements, and leans somewhat on Puck's metaprogramming capabilities.

-

There are several ways to handle errors in Puck. If the error is encoded in the type, one can:

+

Puck's error handling is heavily inspired syntactically by Swift and semantically by the underlying effects system. It uses a combination of monadic error handling and effectful error propagation, with much in the way of syntactic sugar for conversion between the two, and leans somewhat heavily on Puck's metaprogramming capabilities. In comparison to Rust, it is considerably more dynamic by default.

+

There are several ways to handle errors in Puck. If the error is encoded in the type (as an Option or Result type), one can:

  1. match on the error
  2. compactly match on the error with if ... of
  3. propagate the error with ?
  4. throw the error with !
-

If an error is thrown, one must explicitly handle (or disregard) it with a try/catch block or risk runtime failure. This method of error handling may feel more familiar to Java programmers.

-

Errors as Monads

-

Puck provides Option[T] and a Result[T, E] types, imported by default. These are union types and so must be pattern matched upon to be useful: but the standard library provides a bevy of helper functions. +

If the error is thrown (encoded as an effect), one can:

+
    +
  1. ignore the error, propagating it up the call stack
  2. +
  3. recover from the error in a try block
  4. +
  5. convert the error to a Result[T] (monadic form)
  6. +
+

If an error is thrown, one must explicitly handle it at some level of the stack, or risk runtime failure. This method of error handling may feel more familiar to Java programmers. The compiler will warn on - but not enforce catching - such unhandled errors.

+

Errors as monads

+

Puck provides Option[T] and a Result[T, E] types, imported by default. These are union types under the hood and so must be pattern matched upon to be useful: but the standard library provides a bevy of helper functions. Two in particular are of note. The ? operator unwraps a Result or propagates its error up a function call (and may only be used in type-appropriate contexts). The ! operator unwraps an Option or Result directly or throws an exception in the case of None or Error.

-
pub macro `?`[T, E](self: Result[T, E]) =
-  quote:
+
pub macro ?[T, E](self: Result[T, E]) =
+  quote
     match `self`
-    of Okay(x): x
-    of Error(e): return Error(e)
+    of Okay(x) then x
+    of Error(e) then return Error(e)
 
-
pub func `!`[T](self: Option[T]): T =
+
pub func ![T](self: Option[T]): T =
   match self
-  of Some(x): x
-  of None: raise EmptyValue
+  of Some(x) then x
+  of None then raise "empty value"
 
-pub func `!`[T, E](self: Result[T, E]): T =
-  of Okay(x): x
-  of Error(e): raise e
-
-

The utility of the provided helpers in std.options and std.results should not be understated. While encoding errors into the type system may appear restrictive at first glance, some syntactic sugar goes a long way in writing compact and idiomatic code. Java programmers in particular are urged to give type-first errors a try, before falling back on unwraps and try/catch.

-

A notable helpful type is the aliasing of Result[T] to Result[T, ref Err], for when the particular error does not matter. This breaks try/catch exhaustion (as ref Err denotes a reference to any Error), but is particularly useful when used in conjunction with the propagation operator.

-

Errors as Catchable Exceptions

-

Errors raised by raise/throw (or subsequently the ! operator) must be explicitly caught and handled via a try/catch/finally statement. -If an exception is not handled within a function body, the function must be explicitly marked as a throwing function via the yeet prefix (name to be determined). The compiler will statically determine which exceptions in particular are thrown from any given function, and enforce them to be explicitly handled or explicitly ignored.

-

Despite functioning here as exceptions: errors remain types. An error thrown from an unwrapped Result[T, E] is of type E. catch statements, then, may pattern match upon possible errors, behaving similarly to of branches.

-
try:
-  ...
-catch "Error":
-  ...
-finally:
-  ...
+pub func ![T, E](self: Result[T, E]): T =
+  match self
+  of Okay(x) then x
+  of Error(e) then raise e
 
-

This creates a distinction between two types of error handling, working in sync: functional error handling with Option and Result types, and object-oriented error handling with catchable exceptions. These styles may be swapped between with minimal syntactic overhead. Libraries, however, should universally use Option/Result, as this provides the best support for both styles.

- -

Errors and Void Functions

-

Some functions do not return a value but can still fail: for example, setters. -This can make it difficult to do monadic error handling elegantly: one could return a Result[void, E], but...

-
pub func set[T](self: list[T], i: uint, val: T) =
-  if i > self.length:
+

The utility of the provided helpers in std.options and std.results should not be understated. While encoding errors into the type system may appear restrictive at first glance, some syntactic sugar goes a long way in writing compact and idiomatic code. Java programmers in particular are urged to give type-first errors a try, before falling back on unwraps and try/with.

+

A notable helpful type is the aliasing of Result[T] to Result[T, ref Err], for when the particular error does not matter. This breaks match exhaustion (as ref Err denotes a reference to any Error), but is particularly useful when used in conjunction with the propagation operator.

+

Errors as checked exceptions

+

Some functions do not return a value but can still fail: for example, setters. This can make it difficult to do monadic error handling elegantly. One could return a type Success[E] = Result[void, E], but such an approach is somewhat inelegant. Instead: we treat an assert within a function as having an effect: a possible failure, that can be handled and recovered from at any point in the call stack. If a possible exception is not handled within a function body, the function is implicitly marked by the compiler as throwing that exception.

+
pub type list[T] = struct
+  data: ptr T
+  capacity: uint
+  length: uint
+
+@[safe]
+pub func set[T](self: list[T], i: uint, val: T) =
+  if i > self.length then
     raise IndexOutOfBounds
-  self.data.raw_set(offset = i, val)
+  self.data.set(offset = i, val)
+
+var foo = ["Hello", "world"]
+foo.set(0, "Goodbye") # set can panic
+# this propagates an IndexOutOfBounds effect up the call stack.
 
-

Unrecoverable Exceptions

+

Despite functioning here as exceptions: errors remain types. An error thrown from an unwrapped Result[T, E] is of type E. with statements, then, may pattern match upon possible errors, behaving semantically and syntactically similarly to of branches: though notably not requiring exhaustion.

+
try
+  foo.set(0, "Goodbye")
+with IndexOutOfBounds(index) then
+  dbg "Index out of bounds at {}".fmt(index)
+  panic
+finally
+  ...
+
+

This creates a distinction between two types of error handling, working in sync: functional error handling with Option and Result types, and object-oriented error handling with algebraic effects. These styles may be swapped between with minimal syntactic overhead. It is up to libraries to determine which classes of errors are exceptional and best given the effect treatment and which should be explicitly handled monadically. Libraries should tend towards using Option/Result as this provides the best support for both styles (thanks to the ! operator).

+

Unrecoverable exceptions

There exist errors from which a program can not reasonably recover. These are the following:

    -
  • Assertation Failure: a call to an assert function has returned false at runtime.
  • +
  • Assertation Failure: a call to an unhandled assert function has returned false at runtime.
  • Out of Memory: the executable is out of memory.
  • Stack Overflow: the executable has overflowed the stack.
  • any others?
-

They are not recoverable, but the user should be aware of them as possible failure conditions.

-

References: Error Handling in Swift

+

They are not recoverable, and not handled within the effects system, but the user should be aware of them as possible failure conditions.

+
+

References

+
@@ -264,22 +282,6 @@ This can make it difficult to do monadic error handling elegantly: one could ret
- - -- cgit v1.2.3-70-g09d2