<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Tue, Jul 19, 2016 at 9:17 AM Ted F.A. van Gaalen via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">ok, Dave<br>
<br>
<br>
<br>
to me - strings being collections -, all I see is:<br>
trying to find an empty string within another string: There are (at least) two ways of treating this:<br>
<br>
1. return “false” seems to me logically correct, because<br>
there is never an empty string in another string, and an empty string cannot contain another empty string, right?<br>
This has worked very conveniently for NSString in ObjC for more than 20 years, why change it?<br>
Do you know of cases that were problematic with this convention?<br></blockquote><div><br></div><div>Formally-ish speaking, the empty string is a zero-length string contained at every valid index "i" between [startIndex, endIndex].</div><div><br></div><div>A very casual way of showing this (which I won't call a proof, because it's not that formal) is that "ab" is the same string as "a" + "" + "b", so "" is a substring of "ab". Likewise, "a" == "" + "a", therefore "a" has "" has a prefix, and the same argument can be made for the suffix.</div><div><br></div><div>Also, "" + "" == "", so you have "" has the prefix and suffix of "".</div><div><br></div><div>There's simply no reason to disallow this behavior. I've actually gotten bitten by the current wrong behavior in NSString in production before and had to write a special case around it.</div><div> </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
2 throw a runtime error when trying to do this:<br>
str.hasPrefix(“”) // also for hasSuffix, str.contains etc.<br>
<br>
some in-line questions below.<br>
<br>
Kind Regards<br>
<br>
Ted<br>
<br>
<br>
On 19.07.2016, at 16:31, Dave Abrahams <<a href="mailto:dabrahams@apple.com" target="_blank">dabrahams@apple.com</a>> wrote:<br>
><br>
><br>
> on Tue Jul 19 2016, "Ted F.A. van Gaalen" <tedvgiosdev-AT-gmail.com> wrote:<br>
><br>
>> Hi Dave<br>
>><br>
>> “true” ? am I going nuts ? :o)<br>
>><br>
>> var str = "Hello, playground"<br>
>><br>
>> print( str.hasPrefix("”)) // case 1 : false<br>
>><br>
>> print( str.hasSuffix("”)) // case 2 : false<br>
>><br>
>> print("" == “a” ) // case 3 : false<br>
>><br>
>> Currently, all cases above evaluate to “false”<br>
>> i think that is correct,<br>
><br>
> I don't know what to tell you. It may seem intuitively correct to you,<br>
> but others in the thread have laid out the reasons why it is not<br>
> mathematically correct behavior.<br>
Where? I couldn’t find any.<br>
> One other way of rephrasing it: to get<br>
> `false` for str.hasPrefix(""), you actually need special-case code in<br>
> hasPrefix to check for the empty string,<br>
again, maybe it should throw a run-time error instead.<br>
<br>
<br>
> and the caller may well also<br>
> need special-case code to handle the fact that the result is not<br>
> mathematically consistent with other cases on the continuum.<br>
In this context as “continuum” :<br>
are you referring to “sets” or “collections” here?<br>
what other cases?<br>
<br>
> Doing<br>
> things that way doesn't work in practice for real programs.<br>
please explain thank you, because I see no problems at<br>
all with the current NSString-like evaluation…<br>
I’d put an s.isEmpty() in front of it.<br>
><br>
>> because:<br>
>><br>
>> How can an empty string be a prefix or suffix value?<br>
>> as there is no empty string present in a non-empty string.<br>
>><br>
>> Note that if case 1 and case 2 would evaluate to “true”,<br>
>> it would conflict with case 3.<br>
>><br>
>> Can’t imagine that case 3 should in future also result in “true”<br>
>><br>
>> ??<br>
>><br>
>> -----<br>
>><br>
>> Also I hope that changes to String functionality<br>
>> for Swift 4 are not backward breaking changes<br>
>> even the more for string handling, because Strings<br>
>> are heavily used in most apps.<br>
>><br>
>> I am firmly convinced that all future releases of Swift<br>
>> should compile Swift 3 and higher source files without<br>
>> any changes 100 % flawlessly! This prevents early diminishing<br>
>> of Swift’s popularity, especially with those building large<br>
>> codebases using Swift.<br>
>><br>
>> I’ve started a thread about this a week ago,<br>
>> however no one found this important enough to<br>
>> share their opinions with me yet, or were too busy with<br>
>> other subjects to do so.<br>
>><br>
>> Increasingly I have dreams, me<br>
>> programming complete apps in Smalltalk<br>
>> and then automagically generate<br>
>> an macOS, tvOS or iOS runtime app of it.<br>
>><br>
>> (I have also dreams of this world becoming<br>
>> a nice and peaceful placebut that is<br>
>> beyond the context of this forum)<br>
>><br>
>> Kind Regards<br>
>> TedvG<br>
>><br>
>> <a href="http://www.speyer.de" rel="noreferrer" target="_blank">www.speyer.de</a><br>
>><br>
>>> on Mon Jul 18 2016, Kevin Nattinger <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a> <mailto:<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>> wrote:<br>
>>><br>
>>>> I agree, true is definitely the expected behavior. In particular, it<br>
>>>> seems absurd to me that `a.hasPrefix(b)` and `a.hasSuffix(b)` could be<br>
>>>> false when `a == b` is true.<br>
>>><br>
>>> I expect to be reworking Strings for Swift 4, and this is one of the<br>
>>> many things we plan to address.<br>
>>><br>
>>> --<br>
>>> Dave<br>
>>><br>
>>><br>
>><br>
><br>
> --<br>
> Dave<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></div>