Static utility methods for creating Cadenza.Maybe<T> instances.
This type is thread safe.
Use the Maybe.TryParse methods if it is not necessary to know why the value couldn't be parsed. Otherwise, use the Either.TryParse methods.
Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0
See Also: Inherited members from object.
⊟ Public Methods
staticTryConvert<TSource,TResult> (TSource) : Maybe<TResult> Converts the value of type TSource into an instance of type TResult. staticTryParse<T> (string) : Maybe<T> Converts a string into an instance of type T. staticWhen<T> (bool, Func<T>) : Maybe<T> Returns a value-containing Cadenza.Maybe<T> instance or Maybe<T>.Nothing, depending on condition. staticWhen<T> (bool, T) : Maybe<T> Returns a value-containing Cadenza.Maybe<T> instance or Maybe<T>.Nothing, depending on condition.
⊟ TryConvert<TSource,TResult> Generic Method
Converts the value of type TSource into an instance of type TResult.
public static Maybe<TResult> TryConvert<TSource, TResult> (TSource value)⊟ Type Parameters
- TSource
- The type to convert from.
- TResult
- The type to convert to.
⊟ Parameters
- value
- A TSource containing the value to parse into an instance of type TResult.
⊟ Returns
A Cadenza.Maybe<TResult>. If value could be converted into a value of type TResult, then Maybe<TResult>.HasValue will be true and Maybe<TResult>.Value will contain the converted value; otherwise, Maybe<TResult>.HasValue will be false.⊟ Remarks
TryConvert uses Either.TryConvert``2(``0) to convert value into an instance of type TResult.
C# Example Maybe<string> a = Maybe.TryConvert<int, string> (42); Assert.IsTrue (a.HasValue); Assert.AreEqual ("42", a.Value); Maybe<DateTime> b = Maybe.TryConvert<int, DateTime> (42); Assert.IsFalse (b.HasValue);⊟ Requirements
Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0
⊟ TryParse<T> Generic Method
Converts a string into an instance of type T.
⊟ Type Parameters
- T
- The type of object to attempt to parse out of value.
⊟ Parameters
- value
- A string containing the value to parse into an instance of type T.
⊟ Returns
A Cadenza.Maybe<T>. If value could be converted into a value of type T, then Maybe<T>.HasValue will be true and Maybe<T>.Value will contain the converted value; otherwise, Maybe<T>.HasValue will be false.⊟ Remarks
TryParse uses Either.TryConvert``2(``0) to convert the string value into an instance of type T.
C# Example Maybe<int> n; n = Maybe.TryParse<int> (null); Assert.IsFalse (n.HasValue); n = Maybe.TryParse<int> (""); Assert.IsFalse (n.HasValue); n = Maybe.TryParse<int> ("foo"); Assert.IsFalse (n.HasValue); n = Maybe.TryParse<int> ("42.01"); Assert.IsFalse (n.HasValue); n = Maybe.TryParse<int> ("42"); Assert.IsTrue (n.HasValue); Assert.AreEqual (42, n.Value);⊟ Requirements
Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0
⊟ When<T> Generic Method
Returns a value-containing Cadenza.Maybe<T> instance or Maybe<T>.Nothing, depending on condition.
⊟ See Also
⊟ Type Parameters
- T
- The type of value that Cadenza.Maybe<T> should contain.
⊟ Parameters
- condition
- If true, When returns creator().Just(). Otherwise, When returns Maybe<T>.Nothing.
- creator
- A Func<T> which is used to create the value returned when condition is true.
⊟ Returns
A Cadenza.Maybe<T>. If condition is true, then creator is evaluated and a Cadenza.Maybe<T> instance containing the value returned by creator is returned; otherwise, Maybe<T>.Nothing is returned.⊟ Exceptions
Type Reason ArgumentNullException condition is true and creator returned null. ⊟ Remarks
Use this method instead of Maybe.When``1(bool, ``0) when the value you need to return can only be created when condition is true.
C# Example string s = GetSomeString(); int length; // BAD: length = s.Match ( v => Maybe.When (s != null, s.Length), // may throw, if s == null v => Maybe.When (s == null, 0)); // GOOD: length = s.Match ( v => Maybe.When (s != null, () => s.Length), // delays evaluation. v => Maybe.When (s == null, 0));
C# Example bool invoked = false; r = Maybe.When (false, () => {invoked = true; return 42;}); Assert.IsFalse (invoked); Assert.IsFalse (r.HasValue); r = Maybe.When (true, () => {invoked = true; return 42;}); Assert.IsTrue (invoked); Assert.IsTrue (r.HasValue); Assert.AreEqual (42, r.Value);⊟ Requirements
Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0
⊟ When<T> Generic Method
Returns a value-containing Cadenza.Maybe<T> instance or Maybe<T>.Nothing, depending on condition.
⊟ See Also
⊟ Type Parameters
- T
- The type of value that Cadenza.Maybe<T> should contain.
⊟ Parameters
- condition
- If true, When returns value.Just(). Otherwise, When returns Maybe<T>.Nothing.
- value
- A T containing the value to return when condition is true.
⊟ Returns
A Cadenza.Maybe<T>. If condition is true, then a Cadenza.Maybe<T> instance containing the value value is returned; otherwise, Maybe<T>.Nothing is returned.⊟ Exceptions
Type Reason ArgumentNullException condition is true and value returned null. ⊟ Remarks
Use this method instead of Maybe.When``1(bool, Func<``0>) when it doesn't matter of value is immediately evaluated.
C# Example var r = Maybe.When (true, 42); Assert.IsTrue (r.HasValue); Assert.AreEqual (42, r.Value); r = Maybe.When (false, 42); Assert.IsFalse (r.HasValue);⊟ Requirements
Namespace: Cadenza
Assembly: Cadenza (in Cadenza.dll)
Assembly Versions: 0.1.0.0