[5.7] Fix algorithms overload resolution issues #425
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change addresses two overload resolution problems with the
collection-based algorithm methods. First, when RegexBuilder is
imported,
String
gainsRegexComponent
conformance, which meansthe
RegexComponent
-based overloads win for strings, which isundesirable. Second, if a collection has an element type that can
be expressed as an array literal, collection-based methods get
selected ahead of any standard library counterpart. These two problems
combine in a tricky way for
split
andcontains
.For
split
, both the collection-based and regex-based versions needto be marked as
@_disfavoredOverload
so that the problems above canbe resolved. Unfortunately, this sets up an ambiguity once
String
has
RegexComponent
conformance, so theRegexBuilder
moduleincludes separate overloads for
String
andSubstring
that actas tie-breakers. If introduced in the standard library, these would
be a source-breaking change, as they would win over the
Element
-based split when referencing the
split
method, as withlet splitFunction = myString.split
.For
contains
, the same requirements hold, with the addedcomplication that the Foundation overlay defines its own
String.contains(_:)
method with different behavior than includedin these additions. For this reason, the more specific overloads for
String
andSubstring
can't live in theRegexBuilder
module,which creates a problem for source compatibility. As it stands now,
this existing code does not compile with the new algorithm methods
added, as the type of
vowelPredicate
changes from(Character) -> Bool
to(String) -> Bool
: