1

I studied about generics but i still have no understanding, why to use them, when we can use protocols instead?

For example, examine following function:

public static func delete<T>(entity: T, auth: Auth) -> Observable<Void> where T: MSRequestEntity, T: DictConvertable {

// function do something
    }

Ok, we have generic entity T, that is conform to MSRequestEntity and DictConvertable.

But we can simply rewrite this like that:

public static func delete(entity: MSRequestEntity & DictConvertable, auth: Auth) -> Observable<Void> {

    // function do something
        }

So, my question is, in what case should i use generics? All of situations i have imaging could easily be handled with protocols.

Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107

1 Answers1

3

In the case you have provided you are correct. It doesn't necessarily add anything by making it generic.

But take the example where you have some protocol MyProtocol and you want to create a function that takes two of these and returns a third. But the function only works if first and second are of the same type...

func combine(first: MyProtocol, second: MyProtocol) -> MyProtocol {
    // do some combining here.
}

Now it's less well defined because first and second can be of different types here. The only thing that is required is that they conform to the protocol. And what is the return type?

Now consider...

function combine<T: MyProtocol>(first: T, second: T) -> T {
    // do some combining here
}

Now the function is generic but what that adds is that still first and second must conform to the protocol. But now they must be of the same type. And the function will return another item of the same type as first and second.

In this case you definitely benefit from using generics rather than just the protocol.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • thanks for detailed answer, but i think, we can handle such situations by passing two Any objects, provide guard statement to check whether their are same objects, and return type will be also Any. Yeah, thats will be ugly, but still, function will work :) – Evgeniy Kleban Nov 16 '17 at 10:56
  • @EvgeniyKleban but what would the compiler infer as the return type of the first function? TBH, I'm not even sure that first way of writing the function would compile because you're using the Protocol as a Type. Say you pass in two `Strings` and the function checks that they are both the same type... what does the function then return? It could return `Int` or `Float` or `UIView` as long as they conform to `MyProtocol`. So the compiler has no way of type checking it. – Fogmeister Nov 16 '17 at 10:58
  • yes it will, i just did check it in playground – Evgeniy Kleban Nov 16 '17 at 11:00
  • so, we only need to use generics when we want to return an object type or when we want to be sure to pass same type objects? – Evgeniy Kleban Nov 16 '17 at 11:01
  • @EvgeniyKleban are you asking for why to use generics? Or are you looking for people to disagree with when they provide answers to your question? – Fogmeister Nov 16 '17 at 11:01
  • @EvgeniyKleban Also, with your function that checks that the two params are the same type... what happens when they are not the same type? Why even allow that situation to happen in the first place? Why not let the compiler check that for you? – Fogmeister Nov 16 '17 at 11:03
  • im not looking for arguing and i appreciate your help. I just mention that we can handle situations without them, even if that solution is bad one, but working. I have write several apps without generics, and now i'm in a team that use them, and want to understand best practices. – Evgeniy Kleban Nov 16 '17 at 11:08