Class Error<T>
Error holds either a value of type T
or an exception.
Inheritance
System.Object
Error<T>
Inherited Members
System.Object.Equals(System.Object, System.Object)
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Maki
Assembly: Maki.dll
Syntax
Type Parameters
Name |
Description |
T |
Type of value. |
Examples
The following example shows how to use Error to store either a value or an exception.
using Maki;
using System;
namespace Samples
{
class Program
{
// Either returns an int or throws an exception
static int ReturnValueOrThrow()
{
var value = new Random().Next(100);
return value < 50 ? value : throw new Exception();
}
static void Main(string[] args)
{
// Call function, value holds either an int or an exception
Error<int> value = Error.Make(ReturnValueOrThrow);
if (value.HasValue)
{
value = value.Get() * 2;
Console.WriteLine(value.Get());
}
else // value.IsException
{
// Get exception from value, can handle or rethrow
throw value.Exception();
}
}
}
}
Constructors
|
Improve this Doc
View Source
Error(T)
Creates a new Error from the given item.
Declaration
Parameters
Type |
Name |
Description |
T |
item |
Item to initialize the error with. |
|
Improve this Doc
View Source
Error(Exception)
Creates a new Error from the given exception.
Declaration
public Error(Exception e)
Parameters
Type |
Name |
Description |
System.Exception |
e |
Exception to initialize Error with. |
Properties
|
Improve this Doc
View Source
HasValue
True if Error contains a value.
Declaration
public bool HasValue { get; }
Property Value
Type |
Description |
System.Boolean |
|
|
Improve this Doc
View Source
IsError
True if Error contains an exception.
Declaration
public bool IsError { get; }
Property Value
Type |
Description |
System.Boolean |
|
Methods
|
Improve this Doc
View Source
Equals(Object)
Determines whether the specified object is equal to the current object.
Declaration
public override bool Equals(object obj)
Parameters
Type |
Name |
Description |
System.Object |
obj |
The object to compare with the current object. |
Returns
Type |
Description |
System.Boolean |
True if the objects are equal. |
Overrides
System.Object.Equals(System.Object)
|
Improve this Doc
View Source
Exception()
Gets the exception inhabiting the Error.
Declaration
public Exception Exception()
Returns
Type |
Description |
System.Exception |
Exception inhabiting the Error. |
Exceptions
Type |
Condition |
System.InvalidCastException |
Thrown if the Error contains an exception. |
|
Improve this Doc
View Source
Get()
Gets the value inhabiting the Error.
Declaration
Returns
Type |
Description |
T |
Value inhabiting the Error. |
Exceptions
Type |
Condition |
System.InvalidCastException |
Thrown if the Error contains an exception. |
|
Improve this Doc
View Source
GetHashCode()
Returns the hash code for this instance.
Declaration
public override int GetHashCode()
Returns
Type |
Description |
System.Int32 |
Hash code. |
Overrides
System.Object.GetHashCode()
|
Improve this Doc
View Source
MakeException(Exception)
Creates a new Error containing an exception.
Declaration
public static Error<T> MakeException(Exception e)
Parameters
Type |
Name |
Description |
System.Exception |
e |
Exception to hold in the new Error. |
Returns
Type |
Description |
Error<T> |
Error containing the given exception. |
|
Improve this Doc
View Source
MakeValue(T)
Creates a new Error containing a value.
Declaration
public static Error<T> MakeValue(T item)
Parameters
Type |
Name |
Description |
T |
item |
Item to hold in the new Error. |
Returns
Type |
Description |
Error<T> |
Error containing the given item. |
Operators
|
Improve this Doc
View Source
Explicit(Error<T> to T)
Explicit cast from Error to type T
.
Declaration
public static explicit operator T(Error<T> error)
Parameters
Type |
Name |
Description |
Error<T> |
error |
Error to cast from. |
Returns
Exceptions
Type |
Condition |
System.InvalidCastException |
Thrown if the Error doesn't contain a value. |
|
Improve this Doc
View Source
Explicit(Error<T> to Exception)
Explicit cast from Error to Exception.
Declaration
public static explicit operator Exception(Error<T> error)
Parameters
Type |
Name |
Description |
Error<T> |
error |
Error to cast from. |
Returns
Type |
Description |
System.Exception |
|
Exceptions
Type |
Condition |
System.InvalidCastException |
Thrown if the Error doesn't contain an exception. |
|
Improve this Doc
View Source
Implicit(T to Error<T>)
Implicit cast from type T
to Error.
Declaration
public static implicit operator Error<T>(T item)
Parameters
Type |
Name |
Description |
T |
item |
Item to cast from. |
Returns
Type |
Description |
Error<T> |
|
|
Improve this Doc
View Source
Implicit(Exception to Error<T>)
Implicit cast from Exception to Error.
Declaration
public static implicit operator Error<T>(Exception e)
Parameters
Type |
Name |
Description |
System.Exception |
e |
Exception to cast from. |
Returns
Type |
Description |
Error<T> |
|
Extension Methods