It would be nice if there was an IParseable<T> interface providing static T Parse(string toParse) and static bool TryParse(string toParse, out T val)
Optionally there could be IFormattableParseable<T> : IParseable<T>, IFormattable providing static T Parse(string toParse, IFormatProvider provider) and static bool TryParse(string toParse, IFormatProvider provider, out T val) .
The IParseable
Parse are static methods, not instance. So this should need functionals like shape or static interface methods.
This sounds interesting, but might be better to start in the corefxlab repo.
I like this idea 馃槑
When we have static interfaces, I would prefer this
public static interface IParseable<TType>
{
TType Parse(string s);
TType Parse(ReadOnlySpan<char> value)
bool TryParse(string s, out TType result);
bool TryParse(ReadOnlySpan<char> value, out TType result);
}
public static interface IFormattableParseable<TType> : IParseable<TType>
{
TType Parse(string s, NumberStyles style);
TType Parse(ReadOnlySpan<char> value, NumberStyles style);
TType Parse(string s, IFormatProvider provider);
TType Parse(ReadOnlySpan<char> value, IFormatProvider provider);
TType Parse(string s, NumberStyles style, IFormatProvider provider);
TType Parse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider provider);
bool TryParse(string s, NumberStyles style, out TType result);
bool TryParse(ReadOnlySpan<char> value, NumberStyles style, out TType result);
bool TryParse(string s, IFormatProvider provider, out TType result);
bool TryParse(ReadOnlySpan<char> value, IFormatProvider provider, out TType result);
bool TryParse(string s, NumberStyles style, IFormatProvider provider, out TType result);
bool TryParse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider provider, out TType result);
}
@TobiasSekan The usage of ReadOnlySpans would make the implementation more work than needed, and might result in developers refusing the implementation of that interface..
@TheMinefighter
Possible, but I think we need this to increase the performance for heavy usage.
Maybe we can split the ReadOnlySpan<char> in separate interfaces, like this:
public static interface IParseableSpan<TType>
{
TType Parse(ReadOnlySpan<char> value)
bool TryParse(ReadOnlySpan<char> value, out TType result);
}
public static interface IFormattableParseableSpan<TType> : IParseableSpan<TType>
{
TType Parse(ReadOnlySpan<char> value, NumberStyles style);
TType Parse(ReadOnlySpan<char> value, IFormatProvider provider);
TType Parse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider provider);
bool TryParse(ReadOnlySpan<char> value, NumberStyles style, out TType result);
bool TryParse(ReadOnlySpan<char> value, IFormatProvider provider, out TType result);
bool TryParse(ReadOnlySpan<char> value, NumberStyles style, IFormatProvider provider, out TType result);
}
Most helpful comment
Parseare static methods, not instance. So this should need functionals like shape or static interface methods.