0

I'm staring at an ex-colleague's code (on an Extension) that looks like this:

func set<Object: SomeProtocol>(object value: Object) {

What advantages does the signature with generics bring compared to the plain old version?

func set(object value: SomeProtocol) {
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • 2
    Compare [What is the in-practice difference between generic and protocol-typed function parameters?](https://stackoverflow.com/q/38446487/2976878) – Hamish Aug 04 '17 at 20:34
  • Thanks @Hamish, voted up the question and your answer there, very helpful. – Dan Rosenstark Aug 04 '17 at 21:00

1 Answers1

2

As a pattern, your ex-colleague's version is more flexible, because it should work with any type of protocol. Protocols with associated types, for instance, will not work with your version:

// error: protocol 'Equatable' can only be used as a generic constraint because it has Self or associated type requirements
func foo(bar: Equatable) {}
Charles Srstka
  • 16,665
  • 3
  • 34
  • 60