Can adding default value to func parameter be source breaking?

My instinct says no, but I want to check if there is any situation where adding a default value to a function parameter of a public api can be source breaking by any means?

I can easily find instances where removing, or changing the value (from previous default value) can be source breaking.
But cannot see if this will ever have an effect on the major version of an API:

- public func makePizza(_ pizzaType: PizzaType)
+ public func makePizza(_ pizzaType: PizzaType = .capricciosa)

In general introducing new ways to call a function can be source breaking by introducing an ambiguity with existing code that was previously calling a different function with the same name. E.g. if a client library was calling a makePizza() function from a different module (or your own module) then adding the default to the makePizza(_:) method could break that client. I think in this case the break would be relatively narrow because the other function would also have to have a defaulted argument in order to become ambiguous.

7 Likes

Hey @Jumhyn

Thanks for the reply, I understand your example, but if I got it right, if say i had no other function with the same func base name makePizza than there is no other way to possibly add breaking changes by adding the default value?

Unfortunately it’s not just you—if any of your client’s other dependencies have a conflicting makePizza declaration then the problem could rear its head. So you won’t be able to exhaustively determine whether you’re breaking source just by auditing your own code.

3 Likes

Thanks for the clarification @Jumhyn