Helper class used as a return value of methods that may give errors instead of values.

Functions that return useful values or errors could use it to simulate an easy-to use multiple-return.

fun division(a,b: Int): MaybeError[Int, Error]
do
  if b == 0 then return new MaybeError[Int, Error](null, new Error("Arithmetic Error: try to divide {a} by 0"))
  return new MaybeError[Int, Error](a / b, null)
end

assert division(10, 2).is_error  == false
assert division(10, 0).is_error  == true

Clients has to handle the error:

var res = division(10, 2)
if res.is_error then
  print res.error
  exit 1
end
print res.value
assert res.value == 5

Introduced properties

init defaultinit(maybe_value: nullable V, maybe_error: nullable E)

core :: MaybeError :: defaultinit

fun error: E

core :: MaybeError :: error

The error
fun is_error: Bool

core :: MaybeError :: is_error

It there an error?
fun maybe_error: nullable E

core :: MaybeError :: maybe_error

The error, if any
protected fun maybe_error=(maybe_error: nullable E)

core :: MaybeError :: maybe_error=

The error, if any
fun maybe_value: nullable V

core :: MaybeError :: maybe_value

The value, if any
protected fun maybe_value=(maybe_value: nullable V)

core :: MaybeError :: maybe_value=

The value, if any
fun value: V

core :: MaybeError :: value

The value

Redefined properties

redef type SELF: MaybeError[V, E]

core $ MaybeError :: SELF

Type of this instance, automatically specialized in every class
redef fun to_s: String

core $ MaybeError :: to_s

User readable representation of self.

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
init defaultinit(maybe_value: nullable V, maybe_error: nullable E)

core :: MaybeError :: defaultinit

fun error: E

core :: MaybeError :: error

The error
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
fun is_error: Bool

core :: MaybeError :: is_error

It there an error?
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun maybe_error: nullable E

core :: MaybeError :: maybe_error

The error, if any
protected fun maybe_error=(maybe_error: nullable E)

core :: MaybeError :: maybe_error=

The error, if any
fun maybe_value: nullable V

core :: MaybeError :: maybe_value

The value, if any
protected fun maybe_value=(maybe_value: nullable V)

core :: MaybeError :: maybe_value=

The value, if any
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun value: V

core :: MaybeError :: value

The value
package_diagram core::MaybeError MaybeError core::Object Object core::MaybeError->core::Object app::HttpRequestResult HttpRequestResult app::HttpRequestResult->core::MaybeError

Parents

interface Object

core :: Object

The root of the class hierarchy.

Children

class HttpRequestResult

app :: HttpRequestResult

Result of a call to Text::http_get

Class definitions

core $ MaybeError
# Helper class used as a return value of methods that may give errors instead of values.
#
# Functions that return useful values or errors could use it to simulate an easy-to use multiple-return.
#
# ~~~
# fun division(a,b: Int): MaybeError[Int, Error]
# do
#   if b == 0 then return new MaybeError[Int, Error](null, new Error("Arithmetic Error: try to divide {a} by 0"))
#   return new MaybeError[Int, Error](a / b, null)
# end
#
# assert division(10, 2).is_error  == false
# assert division(10, 0).is_error  == true
# ~~~
#
# Clients has to handle the error:
#
# ~~~
# var res = division(10, 2)
# if res.is_error then
#   print res.error
#   exit 1
# end
# print res.value
# assert res.value == 5
# ~~~
class MaybeError[V, E: Error]
	# The value, if any
	var maybe_value: nullable V

	# The error, if any
	var maybe_error: nullable E

	# It there an error?
	fun is_error: Bool do return maybe_error != null

	# The value
	# REQUIRE: `not is_error`
	fun value: V do return maybe_value.as(V)

	# The error
	# REQUIRE: `is_error`
	fun error: E do return maybe_error.as(E)

	redef fun to_s do
		var e = maybe_error
		if e != null then return e.to_s
		return value.as(not null).to_s
	end
end
lib/core/error.nit:42,1--91,3