3

The following function definition is legal Swift:

func doSomething<T: StringProtocol>(value: T = "abc") {
    // ...
}

The compiler is able to determine that the default argument "abc" is a String, and String conforms to StringProtocol.

This code however does not compile:

func doSomething<T: Collection>(value: T = "abc") where T.Element == Character {
    // ...
}

The compiler error:

Default argument value of type 'String' cannot be converted to type 'T'

It seems as though the compiler would have just as much information as in the first case to determine that String is indeed convertible to T. Furthermore, if I remove the default argument and call the function with the same value, it works:

doSomething(value: "abc")

Can this function be written differently so that I can provide the default String argument? Is this a limitation of Swift, or simply a limitation of my mental model?

1
  • 1
    I had no idea that you could do that; thanks for asking!
    – Rob Napier
    Mar 30, 2019 at 13:58

1 Answer 1

2

The significant constraint is T: ExpressibleByStringLiteral. That's what allows something to be initialized from a string literal.

func doSomething<T: Collection>(value: T = "abc")
    where T.Element == Character, T: ExpressibleByStringLiteral {
    // ...
}

As Leo Dabus notes, T.Element == Character is technically not necessary, but removing it changes the meaning. Just because something is a collection and can be initialized by a string literal does not mean that its elements are characters.

It's also worth noting that while all of this is possible, it generally is poor Swift IMO. Swift does not have any way to express what the default type is, so doSomething() in all of these cases causes "Generic parameter 'T' could not be inferred".

The correct solution IMO is an overload, which avoids all of these problems:

func doSomething<T: StringProtocol>(value: T) {
}

func doSomething() {
    doSomething(value: "abc")
}

This allows you to make the default parameter not just "something that can be initialized with the literal "abc"", but what you really mean: the default value is the String "abc".

As a rule, default parameters are just conveniences for overloads, so you can generally replace any default parameter with an explicit overload that lacks that parameter.

2
  • @LeoDabus I don't understand how this would address the default parameter situation. How would you implement the syntax doSomething() as an extension on RangeReplaceableCollection?
    – Rob Napier
    Mar 30, 2019 at 14:09
  • I think the overloading approach is what I am looking for. It might be worth mentioning that my actual use-case is a bit more complicated and does not actually involve strings, but the discussion here is nevertheless very insightful!
    – dalton_c
    Mar 30, 2019 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.