6

I saw some source code in github, like this: functional-swift

We can see there's definition of a struct called Ship, and there're some variable in it. From the following code we can see that there're also some function in it. It is written in the following style:

struct xxx {
}

extension xxx {
    func yyy() {}
}

I can also definite the struct in the following style:

struct xxx {
    func yyy() {}
}

So what the different of the two style? Is there a swift programming style guide?

leizh00701
  • 1,893
  • 5
  • 21
  • 32
  • 1
    Please check this [question](http://stackoverflow.com/questions/28367950/swift-and-using-class-extension) out. I think it covers your question. – 0x416e746f6e Jan 05 '16 at 08:58
  • 2
    It helps to organize your code into groups of related methods, in particular when adopting protocols (as explained in the referenced Q&A). Recommended here: https://github.com/raywenderlich/swift-style-guide#protocol-conformance. – Martin R Jan 05 '16 at 09:03
  • How exactly you group the methods into extensions is a matter of personal taste. I would not define an extension for *each* method, as in the example code that you linked to. – Martin R Jan 05 '16 at 09:10
  • I'm not sure this is a duplicate of that question, because that question is talking about extensions with protocols, which have obvious benefits, whereas this isn't. – GoatInTheMachine Jan 05 '16 at 09:42
  • @GoatInTheMachine: As I understand it, both questions are about when use extensions (and not define all methods directly in the struct/class definition).The other *question* does not mention protocols, so it seems like the same question to me. – It's the *answers* to the other question which tell that protocol conformance is the a major use-case, but also code organization: *"For me it seems completely reasonable since you can use extensions to expose different parts of logic to different extensions."* – Martin R Jan 05 '16 at 09:56

2 Answers2

1

from your example, the first one is a basic struct with an extension

struct xxx {
}

extension xxx {
function yyy() {}
}

the other is a struct with a function built in.

struct xxx {
function yyy() {}
}

imagine you cannot modify the original struct for some reason but you still want to be able to perform function yyy() -> you can extend the class to call the function yyy() without modifying the class itself (or changing the way it behaves elsewhere in your program)

"Extensions can add new functionality to a type, but they cannot override existing functionality." (src: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html)

-> Extensions are useful when you don't have access to a class but you want to add some functionalities. Using extensions, you can compartmentalize your classes and customize what a class can do as needed.

John
  • 465
  • 6
  • 14
0

From the Swift docs:

Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling).

So the usefulness of it is when you want to extend an already existing implementation. Apple has great documentation, and I highly recommend you read the above link.

Community
  • 1
  • 1
rb612
  • 5,280
  • 3
  • 30
  • 68
  • The question here is about defining your own type, not about extending an existing type for which you don't have the source code. – Martin R Jan 05 '16 at 09:06