Skip to content

Commit

Permalink
Simpler & less redundant IFormattable implementation
Browse files Browse the repository at this point in the history
Instead of hopping over another String.Format (and causing an allocation of another format string), delegate to the underlying implementation of IFormattable on the value if it exists, otherwise to Option's own ToString().
  • Loading branch information
atifaziz committed Dec 24, 2016
1 parent f753f1a commit 03f6c03
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions Optional/Option_Maybe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,9 @@ public Option<T> Filter(Func<T, bool> predicate)
/// <returns>The filtered optional.</returns>
public Option<T> NotNull() => hasValue && value == null ? Option.None<T>() : this;

string IFormattable.ToString(string format, IFormatProvider formatProvider)
{
return hasValue
? string.Format(formatProvider, "{0:" + format + "}", value)
: string.Empty;
}
}
string IFormattable.ToString(string format, IFormatProvider formatProvider) =>
!hasValue
? string.Empty
: (value as IFormattable)?.ToString(format, formatProvider) ?? ToString();
}
}

0 comments on commit 03f6c03

Please sign in to comment.