Errors in PureScript
In PureScript, error handling is typically done using the Either
type, which is similar to Go’s approach of returning an error as a separate value. The Left
constructor represents an error, while Right
represents success.
The f
function returns an Either String Int
, where String
is the error type and Int
is the success type. This is analogous to Go’s (int, error)
return type.
Sentinel errors in PureScript are often represented as simple String
values. We define errOutOfTea
and errPower
as constants.
The makeTea
function demonstrates error wrapping by concatenating error messages. This is similar to Go’s fmt.Errorf
with the %w
verb.
In the main
function, we use traverse_
(which is similar to a for
loop) to iterate over lists and handle errors. The case
expressions in PureScript are used for pattern matching, which is how we check for specific error conditions.
To check for specific errors, we use equality comparison with the error strings. This is analogous to Go’s errors.Is
function, although less sophisticated as it doesn’t handle wrapped errors in the same way.
Note that PureScript is a purely functional language, so side effects like printing to the console are wrapped in the Effect
monad.
To run this program, you would typically compile it with the PureScript compiler and then run it with Node.js:
This example demonstrates how PureScript handles errors in a functional way, which is different from Go’s approach but achieves similar goals of explicit error handling and propagation.