/// <summary> /// Tries to get value from Dictionary safely /// </summary> /// <typeparam name="TK"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="dictionary"></param> /// <param name="key"></param> /// <returns></returns> public static Maybe <T> Lookup <TK, T>(this IDictionary <TK, T> dictionary, TK key) { Contract.Requires(dictionary != null); var getter = MaybeFunctionalWrappers.Wrap <TK, T>(dictionary.TryGetValue); return(getter(key)); }
/// <summary> /// Tries to get value from Dictionary safely /// </summary> /// <typeparam name="TK"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="dictionary"></param> /// <param name="key"></param> /// <returns></returns> public static Maybe <T> Lookup <TK, T>(this IDictionary <TK, T> dictionary, TK key) { if (dictionary == null) { throw new ArgumentNullException("dictionary"); } var getter = MaybeFunctionalWrappers.Wrap <TK, T>(dictionary.TryGetValue); return(getter(key)); }
/// <summary> /// Tries to get value from Dictionary safely /// </summary> /// <typeparam name="TK"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="dictionary"></param> /// <param name="key"></param> /// <returns></returns> public static Maybe <T> Lookup <TK, T>(this IDictionary <TK, T> dictionary, TK key) { var getter = MaybeFunctionalWrappers.Wrap <TK, T>(dictionary.TryGetValue); return(getter(key)); }
/// <summary> /// If <paramref name="a"/> can be successfully casted to <typeparamref name="TR"/>, returns the casted one, wrapped as Maybe<TR>, otherwise Nothing /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TR"></typeparam> /// <param name="a"></param> /// <returns></returns> public static Maybe <TR> MaybeCast <T, TR>(this T a) where TR : T { return(MaybeFunctionalWrappers.Catcher <T, TR, InvalidCastException>(o => (TR)o)(a)); }