Try

:= [Ok(ok), Err(err)]
is_ok : Try(_ok, _err) -> Bool

Returns Bool.True if the result indicates a success, else returns Bool.False.

expect Try.Ok(5).is_ok()
is_err : Try(_ok, _err) -> Bool

Returns Bool.True if the result indicates a failure, else returns Bool.False.

expect Try.Err("uh oh").is_err()
from_interpolation : Str, Iter((interpolated, Str)) -> Try(ok, err) where { ok.from_interpolation : Str, Iter((interpolated, Str)) -> Try(ok, err) }

Forwards interpolated string literal assembly through an inner type whose from_interpolation method returns the same Try.

ok_or : Try(ok, _err), ok -> ok

If the result is Ok, returns the value it holds. Otherwise, returns the given default value.

Note: This function should be used sparingly, because it hides that an error happened, which will make debugging harder. Prefer using ? to forward errors or handle them explicitly with match.

expect Try.Err("uh oh").ok_or(42) == 42

expect Try.Ok(7).ok_or(42) == 7
err_or : Try(_ok, err), err -> err

If the result is Err, returns the value it holds. Otherwise, returns the given default value.

expect Try.Err("uh oh").err_or("fallback") == "uh oh"

expect Try.Ok(7).err_or("fallback") == "fallback"
map_ok : Try(a, err), (a -> b) -> Try(b, err)

If the result is Ok, transforms the value it holds by running a conversion function on it. Then returns a new Ok holding the transformed value. If the result is Err, this has no effect. Use Try.map_err to transform an Err.

expect Try.Ok(12.I64).map_ok(|n| -n) == Ok(-12)

expect {
	err : Try(I64, Str)
	err = Err("yipes!")
	err.map_ok(|n| -n) == Err("yipes!")
}

Functions like map are common in Roc; see for example List.map and Set.map.

map_ok! : Try(a, err), (a => b) => Try(b, err)

Like Try.map_ok, but the transform function is effectful. If the argument is an Ok, the effect is run and its return value is wrapped in a new Ok. If the result is Err, the effect is not run and the Err is returned unchanged.

artist_try.map_ok!(|a| SQL.query!("SELECT * FROM albums WHERE artist_id = ?", [a.id]))
map_err : Try(ok, a), (a -> b) -> Try(ok, b)

If the result is Err, transforms the value it holds by running a conversion function on it. Then returns a new Err holding the transformed value. If the result is Ok, this has no effect. Use Try.map_ok to transform an Ok.

expect [].last().map_err(|_| ProvidedListIsEmpty) == Err(ProvidedListIsEmpty)

expect [4].last().map_err(|_| ProvidedListIsEmpty) == Ok(4.0)
map_err! : Try(ok, a), (a => b) => Try(ok, b)

Like Try.map_err, but the transform function is effectful. If the argument is an Err, the effect is run and its return value is wrapped in a new Err. If the result is Ok, the effect is not run and the Ok is returned unchanged.

# Log the failure to the database only when the request errored.
request.map_err!(|e| SQL.execute!("INSERT INTO errors (message) VALUES (?)", [e.message]))
is_eq : Try(ok, err), Try(ok, err) -> Bool where { ok.is_eq : ok, ok -> Bool, err.is_eq : err, err -> Bool }

Returns Bool.True if the two Try values are the same variant (Ok or Err) and their contents are pairwise equal. Otherwise, returns Bool.False.

to_hash : Try(ok, err), Hasher -> Hasher where { ok.to_hash : ok, Hasher -> Hasher, err.to_hash : err, Hasher -> Hasher }

Feed a Try into a Hasher.