A
A
Andrey Bogdanov2020-01-07 20:41:20
Swift
Andrey Bogdanov, 2020-01-07 20:41:20

What is the difference in declaring a generic type in Swift?

What is the difference between these two functions? I did not find an answer on the Internet.
5e14c28e4fe74691415580.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2020-01-07
@scaler

In the form in which you gave them, the functions do not differ .
The parameter on the right specifies the type of T , in the tutorials this is called a type constraint .

static func max1<T:Comparable>(a: T, b: T) -> T {
     return a < b ? b : a
}
    
static func max2<T:Comparable>(a: T, b: T) -> T where T : Equatable {
     return a < b ? b : a
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question