8000 Propose omitting type parameters when possible. · smashcoding/swift-style-guide@b0f391f · GitHub
[go: up one dir, main page]

Skip to content

Commit b0f391f

Browse files
committed
Propose omitting type parameters when possible.
1 parent ed32b74 commit b0f391f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,30 @@ _Rationale:_ Value types are simpler, easier to reason about, and behave as expe
186186
Classes should start as `final`, and only be changed to allow subclassing if a valid need for inheritance has been identified. Even in that case, as many definitions as possible _within_ the class should be `final` as well, following the same rules.
187187

188188
_Rationale:_ Composition is usually preferable to inheritance, and opting _in_ to inheritance hopefully means that more thought will be put into the decision.
189+
190+
191+
#### Omit type parameters where possible
192+
193+
Methods of parameterized types can omit type parameters on the receiving type when they’re identical to the receiver’s. For example:
194+
195+
```swift
196+
struct Composite<T> {
197+
198+
func compose(other: Composite<T>) -> Composite<T> {
199+
return Composite<T>(self, other)
200+
}
201+
}
202+
```
203+
204+
could be rendered as:
205+
206+
```swift
207+
struct Composite<T> {
208+
209+
func compose(other: Composite) -> Composite {
210+
return Composite(self, other)
211+
}
212+
}
213+
```
214+
215+
_Rationale:_ Omitting redundant type parameters clarifies the intent, and makes it obvious by contrast when the returned type takes different type parameters.

0 commit comments

Comments
 (0)
0