-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bool does not implement IParsable<TSelf> #78523
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
This is a subset of #67388. |
Not going to happen. Servicing releases do not get new APIs. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsDescriptionI'm writing a parser and this one is quite problematic. For the most basic type, a I realize that there is little need for a public static bool Parse(string value, IFormatProvider? _) => Parse(value);
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? _, [MaybeNullWhen(false)] out TSelf result)
{
return TryParse(s, out result);
} Reproduction StepsRunning internal class Program
{
static void Main(string[] args)
{
Cast<int>("123"); // OK
Cast<bool>("true"); // CS0315
}
public static TSelf Cast<TSelf>(string s) where TSelf : IParsable<TSelf> => TSelf.Parse(s, null);
} Expected behavior
Actual behavior
Regression?No Known WorkaroundsWherever a method expects internal class Program
{
static void Main(string[] args)
{
Cast<int>("123");
CastBool("true"); // CS0315
}
public static TSelf Cast<TSelf>(string s) where TSelf : IParsable<TSelf> => TSelf.Parse(s, null);
public static bool CastBool(string s) => bool.Parse(s);
} Configuration.NET 7.0 Other information
|
Yeah I just found myself needing |
Can we expect this to be fixed in .NET 8? |
Depends on what API review says for the API. |
Adding the interface decl on bool and implementing the methods explicitly seems reasonable, though we should go all the way to ISpanParsable namespace System;
public partial struct Boolean : ISpanParsable<Boolean>
{
// ISpanParsable and IParsable members explicitly implemented (if they're not already present)
} |
Will have a PR up covering this and #78842 shortly |
Description
I'm writing a parser and this one is quite problematic. For the most basic type, a
boolean
, the new IParsable<TSelf> interface is not available. This makes no sense.I realize that there is little need for a
bool.Parse(string, IFormatProvider?)
but since the second parameter may benull
, it would be a no-brainer to implement. See Boolean.cs(228). Just add the interface, and these methods:Reproduction Steps
Running
VS 17.5
targeting.NET 7.0
, simply create a console app with the code below.Expected behavior
Boolean
implementsIParsable<TSelf>
, just like all other primitive types.Actual behavior
Boolean
does not implementIParsable<TSelf>
.Regression?
No
Known Workarounds
Wherever a method expects
IParsable<T>
you can duplicate that method to handlebool
specifically. E.g.:Configuration
.NET 7.0
Windows 11 (x64)
Other information
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Boolean.cs#L24
The text was updated successfully, but these errors were encountered: