Runtime: Feature Request: IParseable<T> interface for all types implementing Parse() and TryParse()

Created on 19 Jun 2018  路  5Comments  路  Source: dotnet/runtime

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 interface should be implemented by all types providing the Parse() and TryParse() method, IFormattableParseable should be implemented by all types additionally implementing IFormattable. T should always be the type itself.

api-needs-work api-suggestion area-System.Runtime

Most helpful comment

Parse are static methods, not instance. So this should need functionals like shape or static interface methods.

All 5 comments

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);
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

matty-hall picture matty-hall  路  3Comments

Timovzl picture Timovzl  路  3Comments

EgorBo picture EgorBo  路  3Comments

bencz picture bencz  路  3Comments

jkotas picture jkotas  路  3Comments