/// <summary>
 /// Subtract the Some(x) of one option from the Some(y) of another.
 /// For numeric values the behaviour is to find the difference between the Somes (lhs - rhs)
 /// For Lst values the behaviour is to remove items in the rhs from the lhs
 /// For Map or Set values the behaviour is to remove items in the rhs from the lhs
 /// Otherwise if the T type derives from ISubtractable then the behaviour
 /// is to call lhs.Subtract(rhs);
 /// </summary>
 /// <param name="lhs">Left-hand side of the operation</param>
 /// <param name="rhs">Right-hand side of the operation</param>
 /// <returns>lhs - rhs</returns>
 public static T?subtract <T>(T?lhs, T?rhs) where T : struct
 {
     if (!lhs.HasValue)
     {
         return(rhs);
     }
     if (!rhs.HasValue)
     {
         return(lhs);
     }
     return(TypeDesc.Subtract(lhs.Value, rhs.Value, TypeDesc <T> .Default));
 }
示例#2
0
        /// <summary>
        /// Subtract the Some(x) of one option from the Some(y) of another.
        /// For numeric values the behaviour is to find the difference between the Somes (lhs - rhs)
        /// For Lst values the behaviour is to remove items in the rhs from the lhs
        /// For Map or Set values the behaviour is to remove items in the rhs from the lhs
        /// Otherwise if the T type derives from ISubtractable then the behaviour
        /// is to call lhs.Subtract(rhs);
        /// </summary>
        /// <param name="lhs">Left-hand side of the operation</param>
        /// <param name="rhs">Right-hand side of the operation</param>
        /// <returns>lhs - rhs</returns>
        public OptionUnsafe <T> Subtract(OptionUnsafe <T> rhs)
        {
            var self = IsNone
                ? TypeDesc <T> .Default.HasZero
                    ? Some(TypeDesc <T> .Default.Zero <T>())
                    : this
                : this;

            if (self.IsNone)
            {
                return(this);              // zero - rhs = undefined (when HasZero == false)
            }
            if (rhs.IsNone)
            {
                return(this);              // lhs + zero = lhs
            }
            return(TypeDesc.Subtract(self.Value, rhs.Value, TypeDesc <T> .Default));
        }
示例#3
0
 public NewType <T> Subtract(NewType <T> rhs) =>
 GetType() == rhs.GetType()
         ? (NewType <T>) NewType.Construct(GetType(), TypeDesc.Subtract(Value, rhs.Value, TypeDesc <T> .Default))
         : failwith <NewType <T> >("Mismatched NewTypes in subtract");