-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add boolean parse #51026
Add boolean parse #51026
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Syntax Javaimport java.lang.*;
public class Program {
public static void main(String[] args) {
System.out.println(Boolean.parseBoolean("true")); //true
System.out.println(Boolean.parseBoolean("false")); // false
System.out.println(Boolean.parseBoolean("TRUE")); //true
System.out.println(Boolean.parseBoolean("FALSE")); // false
System.out.println(Boolean.parseBoolean("0")); //false
System.out.println(Boolean.parseBoolean("1")); // false
System.out.println(Boolean.parseBoolean("y")); // false
System.out.println(Boolean.parseBoolean("n")); // false
}
} Syntax CSharpusing System;
public class Program
{
public static void Main()
{
Console.WriteLine(bool.Parse("true")); // True
Console.WriteLine(bool.Parse("false")); // False
Console.WriteLine(bool.Parse("TRUE")); // True
Console.WriteLine(bool.Parse("FALSE")); // False
Console.WriteLine(bool.Parse("0")); // FormatException
Console.WriteLine(bool.Parse("1")); // FormatException
Console.WriteLine(bool.Parse("y")); // FormatException
Console.WriteLine(bool.Parse("n")); // FormatException
}
} Syntax Dart proposalclass Program
{
static void Main()
{
print(bool.parse("true")); // True
print(bool.parse("false")); // False
print(bool.parse("TRUE")); // True
print(bool.parse("FALSE")); // False
print(bool.parse("0")); // FormatException
print(bool.parse("1")); // FormatException
print(bool.parse("y")); // FormatException
print(bool.parse("n")); // FormatException
}
}
``
|
/cc @lrhn |
It's a valid addition. I'm not sure it's really valuable enough, but it's also very low-cost (static method, easily tree-shaken if not used). I'd probably want the case-insensitivity to be optional, so external static bool parse(String source, {bool caseInsensitive = false});
external static bool? tryParse(String source, {bool caseInsensitive = false}); Documentation should be clear that the only accepted inputs are the ASCII letter sequences "true" and "false", optionally allowing upper case letters as well as lower case, in any combination. The important case is to recognize precisely the output of On the other hand, without case insensitivity, it's so trivial a function that it's not particularly valuable. The |
@lrhn Thank you, I will make the suggested changes and add your Just confirms me about the class Program
{
static void Main()
{
if(bool.parse("true")){
//.. ok
}
//FormatException
if(bool.parse("FalSe", caseInsensitive: true)){
}
}
} |
I'd expect no format exception when It's a little iffy, because "case-insesitive" is a negative-like word, so it probably should be I guess in this case, it's a trade-off. The more I look at it, I think I prefer external static bool? tryParse(String source, {bool caseSensitive = true}); Then |
@lrhn Could you check the new implementation? Commit: 9974259 Syntax Dart proposal void main() {
print(bool.parse("true")); // true
print(bool.parse("false")); //false
print(bool.parse("TRUE")); // FormatException
print(bool.parse("FALSE")); //FormatException
print(bool.parse("True", caseSensitive: false)); // true
print(bool.parse("False", caseSensitive: false)); // false
} |
Please add tests. There are tests for other primitive classes, e.g. This suggests
I do not think that this function needs to have implementations specialized to the different runtimes. Something like the following should be sufficient: // sdk/lib/core/bool.dart
class bool {
/// doc here
bool parse(String source, {bool caseSensitive = true}) {
return tryParse(source, caseSensitive: caseSensitive) ??
(throw FormatError(...));
}
/// doc here
bool? tryParse(String source, {bool caseSensitive = true}) {
if (source == 'true') return true;
if (source == 'false') return false;
if (caseSensitive) return null;
return tryParse(source.toLowerCase());
}
} I am not totally convinced that we should have case-insensitive parsing, but we do have case-insensitive parsing of numbers i.e. accepting numbers that would not be produced by some variant of |
@rakudrama @lrhn, I particularly like the C# implementation a lot, it works very well. I believe that in dart we can do something like the example below: void main() {
print(bool.parse("true")); // true
print(bool.parse("false")); //false
print(bool.parse("TRUE")); // true
print(bool.parse("FALSE")); // false
print(bool.parse(" True ")); // true
print(bool.parse(" False ")); // false
print(bool.parse("")); // FormatException
print(bool.parse("t r u e")); //FormatException
print(bool.parse("truee")); //FormatException
print(bool.parse("y")); // FormatException
print(bool.parse("0")); // FormatException
print(bool.parse("1")); // FormatException
}
|
About the example: void main() {
print(bool.parse("true")); // true
print(bool.parse("false")); //false
print(bool.parse("TRUE")); // true
print(bool.parse("FALSE")); // false
print(bool.parse(" True ")); // true
print(bool.parse(" False ")); // false
print(bool.parse("")); // FormatException
print(bool.parse("t r u e")); //FormatException
print(bool.parse("truee")); //FormatException
print(bool.parse("y")); // FormatException
print(bool.parse("0")); // FormatException
print(bool.parse("1")); // FormatException
} My experience from The one place where I'd have done something else for I'm a little split on whether case sensitivity should default to true or false. I'd like to say |
@rakudrama , @lrhn 779da89 add the tests, sorry for the delay. |
I also had a review I had forgotten to publish, so sorry about that delay too. |
421faec
to
ff6fb9d
Compare
@srburton please see additional feedback in https://dart-review.googlesource.com/c/sdk/+/279746 |
https://dart-review.googlesource.com/c/sdk/+/279746 has been updated with the latest commits from this pull request. |
1 similar comment
https://dart-review.googlesource.com/c/sdk/+/279746 has been updated with the latest commits from this pull request. |
https://dart-review.googlesource.com/c/sdk/+/279746 has been updated with the latest commits from this pull request. |
1 similar comment
https://dart-review.googlesource.com/c/sdk/+/279746 has been updated with the latest commits from this pull request. |
Hello, I see that many flutter users complain that they don't have a native parse for boolean, so I decided to create this feature.