O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Protocols and Extensions

Since protocols in Swift are implemented directly in the class/struct, during definition or via extension, the protocol implementation ends up changing the class/struct globally. To see the problem with this, imagine that you have two different libraries relying on different JSON protocols:

 protocol JSONA {
  func toJSON(precision: Integer) -> String
 }
 protocol JSONB {
  func toJSON(scale: Integer) -> String
 }

If the preceding protocols have different specifications on how the precision argument must be handled, we will be able to implement only one of the two previous protocols. That’s because implementing any of the protocols above means adding a toJSON(Integer) method to the class/struct, and there can be only ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required