Try Install Learn Blog API Packages GitHub
Pages

Result

Utility function for the Result type.

Functions

error
(
input
:
a
)
:
Result(a, b)

Returns a new error result.

(Result.error("error")
|> Result.isError()) == true
flatMap
(
func
:
Function(a, Result(error, b))
input
:
Result(error, a)
)
:
Result(error, b)
isError
(
input
:
Result(a, b)
)
:
Bool

Returns true if the result is an error.

(Result.error("error")
|> Result.isError()) == true
isOk
(
input
:
Result(a, b)
)
:
Bool

Returns true if the result is ok.

(Result.ok("ok")
|> Result.isOk()) == true
join
(
input
:
Result(error, Result(error, value))
)
:
Result(error, value)
map
(
func
:
Function(b, c)
input
:
Result(a, b)
)
:
Result(a, c)

Maps over the value of the given result.

(Result.error("error")
|> Result.map(\item : String => item + "1")) == Result.error("error")

(Result.ok("ok")
|> Result.map(\item : String => item + "1")) == Result.ok("ok1")
mapError
(
func
:
Function(a, c)
input
:
Result(a, b)
)
:
Result(c, b)

Maps over the error of the given result.

(Result.error("error")
|> Result.mapError(\item : String => item + "1")) == Result.error("error1")

(Result.ok("ok")
|> Result.mapError(\item : String => item + "1")) == Result.ok("ok")
ok
(
input
:
a
)
:
Result(b, a)

Returns a new ok result.

(Result.ok("ok")
|> Result.isOk()) == true
toMaybe
(
result
:
Result(a, b)
)
:
Maybe(b)

Converts the given result into a maybe.

(Result.ok("blah")
|> Result.toMaybe()) == Maybe.just("blah")

(Result.error("blah")
|> Result.toMaybe()) == Maybe.nothing()
withDefault
(
defaultValue
:
b
input
:
Result(a, b)
)
:
b

Returns the value of the given result or the default value if it's an error.

(Result.error("error")
|> Result.withDefault("a")) == "a"

(Result.ok("ok")
|> Result.withDefault("a")) == "ok"
withError
(
defaultError
:
a
input
:
Result(a, b)
)
:
a

Returns the error of the given result or the default value if it's an ok.

(Result.error("error")
|> Result.withDefault("a")) == "error"

(Result.ok("ok")
|> Result.withDefault("a")) == "a"