Add conversion String -> Bool


(Arsen Gasparyan) #1

Hello,

It would be great if we had `init?(_ text: String)` for Bool class. Because
sometime when you're processing JSON/XML it can help you a lot.

Examples:

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

via https://bugs.swift.org/browse/SR-1282

Thanks.


(Brent Royal-Gordon) #2

It would be great if we had `init?(_ text: String)` for Bool class. Because sometime when you're processing JSON/XML it can help you a lot.

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

(I'm not quite sure what's going on with the "scheduled for review" at the top, so let's leave that bit aside for now.)

···

--
Brent Royal-Gordon
Architechies


(Leonardo Pessoa) #3

I think it would be interesting if it could also recognise "1" and "yes" too as it can be useful in some cases. Perhaps have an option for this converter to be customised.

L

···

-----Original Message-----
From: "Arsen Gasparyan via swift-evolution" <swift-evolution@swift.org>
Sent: ‎04/‎06/‎2016 10:10 AM
To: "swift-evolution@swift.org" <swift-evolution@swift.org>
Subject: [swift-evolution] Add conversion String -> Bool

Hello,

It would be great if we had `init?(_ text: String)` for Bool class. Because sometime when you're processing JSON/XML it can help you a lot.

Examples:

Bool("true") -> true
Bool("false") -> false
Bool("TrUE") -> true
Bool(" true ") -> nil
Bool("1") -> nil
Bool("Y") -> nil
Bool("whatever") -> nil via https://bugs.swift.org/browse/SR-1282

Thanks.


(Chris Lattner) #4

Too much going on right now, and I lost track of this. I’ll kick off the review.

-Chris

···

On Jun 4, 2016, at 7:22 AM, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:

It would be great if we had `init?(_ text: String)` for Bool class. Because sometime when you're processing JSON/XML it can help you a lot.

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

(I'm not quite sure what's going on with the "scheduled for review" at the top, so let's leave that bit aside for now.)


(Mirek Elsner) #5

Since the XML and JSON related objects are part of Foundation, I think it would be great to have that functionality in Foundation as well. For “true”, “false”, “1” and “0” (in addition to 1 and 0).

Mirek

···

On Jun 4, 2016, at 06:10, Arsen Gasparyan via swift-evolution <swift-evolution@swift.org> wrote:

Hello,

It would be great if we had `init?(_ text: String)` for Bool class. Because sometime when you're processing JSON/XML it can help you a lot.

Examples:

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

via https://bugs.swift.org/browse/SR-1282

Thanks.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Félix Cloutier) #6

I'm not particularly in favor of a String initializer for booleans. In fact, I'm not particularly in favor of a String initializer for any numeric type.

Swift's String design ensures that developers can't make assumptions about encodings and a more international alphabet, which is a net win for natural languages that have a more complex alphabet than English. It is 2016 and you wouldn't believe how often my name is returned to me as "Felix", "Flix", "F?lix", "Félix", or that I'm told that my name is "invalid". I suspect that some of you could be guilty of name-validating regular expressions that look like [a-zA-Z\-].

(I understand that English keyboards don't have acute accents and that people write it Felix. However, it is less understandable when a machine processes my name and I wrote it right in the first place.)

Providing numeric type initializers that accept a String and interpret it with the C locale is a step backwards in the direction of supporting non-English languages. Even if you put a big red flag around the method and say "this uses the C locale and is improper for international inputs", the fact is that the C locale is essentially the American locale, and it's been proven over and over that humans will use the wrong tool if it is simpler than the right tool.

There is value in reading interchange formats like JSON. However, these languages define their own grammar and syntax. I see little benefit in letting that grammar and syntax leak into the realm of parsing values in the general case.

Félix

···

Le 4 juin 2016 à 11:38:04, Mirek Elsner via swift-evolution <swift-evolution@swift.org> a écrit :

Since the XML and JSON related objects are part of Foundation, I think it would be great to have that functionality in Foundation as well. For “true”, “false”, “1” and “0” (in addition to 1 and 0).

Mirek

On Jun 4, 2016, at 06:10, Arsen Gasparyan via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Hello,

It would be great if we had `init?(_ text: String)` for Bool class. Because sometime when you're processing JSON/XML it can help you a lot.

Examples:

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

via https://bugs.swift.org/browse/SR-1282

Thanks.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Arsen Gasparyan) #7

Agree about "1" and "0" because it's a valid XML boolean value. But I don't
know where you can find "yes" / "no" value.

http://www.w3schools.com/xml/schema_dtypes_misc.asp
Note: Legal values for boolean are true, false, 1 (which indicates true),
and 0 (which indicates false).

···

On Sat, Jun 4, 2016 at 4:14 PM Leonardo Pessoa <me@lmpessoa.com> wrote:

I think it would be interesting if it could also recognise "1" and "yes"
too as it can be useful in some cases. Perhaps have an option for this
converter to be customised.

L
------------------------------
From: Arsen Gasparyan via swift-evolution <swift-evolution@swift.org>
Sent: ‎04/‎06/‎2016 10:10 AM
To: swift-evolution@swift.org
Subject: [swift-evolution] Add conversion String -> Bool

Hello,

It would be great if we had `init?(_ text: String)` for Bool class.
Because sometime when you're processing JSON/XML it can help you a lot.

Examples:

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

via https://bugs.swift.org/browse/SR-1282

Thanks.


(Taras Zakharko) #8

IMO, that is too idiosyncratic to be a standard language feature. You should extend Bool based on the specific needs of your application. Thats what the extensions are for.

Best,

t.

···

On 04 Jun 2016, at 15:10, Arsen Gasparyan via swift-evolution <swift-evolution@swift.org> wrote:

Hello,

It would be great if we had `init?(_ text: String)` for Bool class. Because sometime when you're processing JSON/XML it can help you a lot.

Examples:

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

via https://bugs.swift.org/browse/SR-1282

Thanks.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(David Sweeris) #9

You may be right, but I'd imagine such a conversion function would be in pretty much any library for serializing/deserializing data.

I can't recall if this has been discussed before, but are we committed to One Standard Library to Rule Them All, or might we consider having a "standard" library and a "common extensions" library? (Personally, I'd prefer them to be split up so that 3rd-party library authors can import only what they need, but maybe that's just me.)

- Dave Sweeris

···

On Jun 4, 2016, at 08:26, Taras Zakharko via swift-evolution <swift-evolution@swift.org> wrote:

IMO, that is too idiosyncratic to be a standard language feature. You should extend Bool based on the specific needs of your application. Thats what the extensions are for.

Best,

t.

On 04 Jun 2016, at 15:10, Arsen Gasparyan via swift-evolution <swift-evolution@swift.org> wrote:

Hello,

It would be great if we had `init?(_ text: String)` for Bool class. Because sometime when you're processing JSON/XML it can help you a lot.

Examples:

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

via https://bugs.swift.org/browse/SR-1282

Thanks.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution