} // op_Implicit(optional) /// <summary> /// Compares the current object with another object of the same type. /// <c>None</c> is considered to be less than all <c>Some(value)</c>; if /// both objects have values, they are compared. /// </summary> /// /// <returns> /// A 32-bit signed integer that indicates the relative order of the /// objects being compared. The return value has the following /// meanings: Value Meaning Less than zero This object is less than the /// <paramref name="other" /> parameter.Zero This object is equal to /// <paramref name="other" />. Greater than zero This object is greater /// than <paramref name="other" />. /// </returns> /// /// <param name="other">An object to compare with this object.</param> /// <remarks>Requires that <typeparamref name="T"/> is /// <see cref="IComparable{T}"/>. Null is considered to be less than /// <c>None</c>.</remarks> public int CompareTo(OptionalNotNull <T> other) { return(HasValue ? (other.HasValue ? Comparer <T> .Default.Compare(_value, other._value) : 1) : (other.HasValue ? -1 : 0)); } // CompareTo(other)
/// <summary> /// Asserts the value is not null. /// </summary> /// <returns><c>Some(Value)</c> if this optional has a value and this value is not /// <c>null</c>; <c>None</c> if this optional doesn't have a value.</returns> /// <exception cref="InvalidOperationException">when the value exists and is /// <c>null</c>.</exception> public OptionalNotNull <T> AssertNotNull() { // ReSharper disable CompareNonConstrainedGenericWithNull if (HasValue) { if (Value == null) { throw new InvalidOperationException( "Value can't be null", new ArgumentNullException("Value")); } else { return(OptionalNotNull.Some(Value)); } } else { return(OptionalNotNull.None <T>()); } // ReSharper restore CompareNonConstrainedGenericWithNull }
} // op_Inequality(one, other) ///<summary> ///Indicates whether the current object is equal to another object of the same type. ///</summary> /// ///<returns> ///true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false. ///</returns> /// ///<param name="other">An object to compare with this object.</param> /// <remarks>Requires that <typeparamref name="T"/> is <see cref="IEquatable{T}"/>.</remarks> public bool Equals(OptionalNotNull <T> other) { return(HasValue ? other.HasValue && _value.Equals(other._value) : !other.HasValue); } // Equals(other)
} // OrElse(@default) /// <summary> /// If this optional has a value, returns itself; else returns /// <paramref name="@default"/>. /// </summary> /// <param name="default">The @default.</param> /// <returns></returns> public OptionalNotNull <T> OrElse(OptionalNotNull <T> @default) { return(HasValue ? this : @default); } // OrElse(@default)