TruMan1
TruMan1

Reputation: 36158

Difference between `func<T: Type>` and `func<T>(..) where T: Type`?

When restricting the type of a generic argument, why is there 2 ways in Swift to do it?

  1. func<T: Type>
  2. func<T>(..) where T: Type

Is there a difference between the two or is this just legacy syntax left behind?

Upvotes: 2

Views: 152

Answers (2)

Daniel T.
Daniel T.

Reputation: 33979

They are identical in function. The first version is there for legacy reasons, it's a holdover from Swift 1. The second version is newer in conception and considered "more proper" by Apple.

Upvotes: 1

Alexander
Alexander

Reputation: 63369

There is no difference, and I'm not aware of a convention.

Personally, prefer the first option, unless it makes the rest of the line have to line-break, in which case I use the second option instead.

Upvotes: 3

Related Questions