[swift-evolution] Add conversion String -> Bool

Félix Cloutier felixcca at yahoo.ca
Sat Jun 4 14:52:47 CDT 2016


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 at 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 at swift.org <mailto:swift-evolution at 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 <https://bugs.swift.org/browse/SR-1282>
>> 
>> Thanks.
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160604/48cd2b57/attachment.html>


More information about the swift-evolution mailing list