Skip to content
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

[SR-1282] Add conversion String -> Bool #4356

Open
swift-ci opened this issue Apr 21, 2016 · 4 comments
Open

[SR-1282] Add conversion String -> Bool #4356

swift-ci opened this issue Apr 21, 2016 · 4 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-1282
Radar None
Original Reporter sdavids (JIRA User)
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s Foundation, Standard Library
Labels New Feature, LanguageFeatureRequest
Assignee None
Priority Medium

md5: 86692dd3cbe368d50f6ddd85e61d06bb

Issue Description:

Please add a conversion from String to Bool.

init?(_ text: String)

The conversion method should be case insensitive.

Examples:

Bool("true") -> true
Bool("false") -> false
Bool("TrUE") -> true
Bool(" true ") -> nil
Bool("1") -> nil
Bool("Y") -> nil
Bool("whatever") -> nil 

This would be useful for JSON/XML processing.

@belkadan
Copy link

This would need to go through the Swift Evolution Process.

@karwa
Copy link
Contributor

karwa commented Jun 24, 2016

This looks like it could be covered by SE-0089's LosslessStringConvertible

https://github.com/apple/swift-evolution/blob/master/proposals/0089-rename-string-reflection-init.md

@swift-ci
Copy link
Contributor Author

Comment by Markus Hanauska (JIRA)

You seriously want to add a complete language feature, just because it helps you with one very specific use case? A use case that most people never face as they will use a ready-to-use JSON parser for such a task? And that all for a feature you could implement yourself with maybe 10 lines of code? Yup, that's also a way to bloat a language beyond good and evil. If we start like that, then we must add another 2 million features that only server maybe one single use case and that each could have easily be implemented by the programmer himself. Where can I downvote on this?

@swift-ci
Copy link
Contributor Author

swift-ci commented Dec 8, 2016

Comment by Pascal (JIRA)

This works in Swift 3 (but does not tolerate spelling variants as proposed by OP).

Welcome to Apple Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1). Type :help for assistance.
  1> Bool("true")
$R0: Bool? = true
  2> Bool("false")
$R1: Bool? = false
  3> Bool("TrUE")
$R2: Bool? = nil
  4> Bool(" true ")
$R3: Bool? = nil
  5> Bool("1")
$R4: Bool? = nil
  6> Bool("Y")
$R5: Bool? = nil
  7> Bool("whatever")
$R6: Bool? = nil

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants