-1

I have this protocol hierarchy:

protocol A {}

protocol B: A {}

what will happen if I have the 2 following funcs:

func myFunc<T : A where T: B>( object: T){ ... }
func myfunc<T : A>( object: T){ ... }

Which function will be executed if I call

myFunc( object: myInstance ) 

with myInstance conforming to protocol B. In this case myInstance matches the 2 constraints.

Thanks

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
t4ncr3d3
  • 615
  • 1
  • 8
  • 17
  • 2
    You can try it yourself you know ;) – Hamish Jun 06 '16 at 21:01
  • Wanted to know what the general rule was. – t4ncr3d3 Jun 06 '16 at 22:00
  • I figured as much and was actually in the middle of writing an answer when Daniel swooped in. Although you should note that this question only ever asks for "which function will be executed", not why a given function will be executed. It always helps to be explicit in exactly what you're asking. – Hamish Jun 06 '16 at 22:10

1 Answers1

2

The general rule is that the compiler tries to select the most specific / most constrained overload.

For your example above, it would be the first version of myFunc

Daniel Hall
  • 13,457
  • 4
  • 41
  • 37