Multiple matching generic functions

I am having trouble finding documentation on the following topic and it is probably caused by me just not knowing what the look for.  The following code works and does what I hoped.  I simply want to know the rules that swift uses when multiple matching methods are available.

 

struct DataEntry {
  var x = 10.2
  init() {}
}
extension DataEntry where T: Numeric {
  func What(theHeck: T) {
    print("In Numeric Protocol version of function")
  }
}
extension DataEntry where T == Double {
  func What(theHeck: T) {
    print("In Double version of function")
  }
}
var x = DataEntry()
x.What(theHeck: 10)
var y = DataEntry()
y.What(theHeck: 10.2)     // There are two matching functions because Double is also Numeric

The output for this is:

In Numeric Protocol version of function

In Double version of function

which is what I had hoped.  My question is about the rules that swift uses when this happens so that I can ensure that I don’t run into problems later. 

Powered by WPeMatico

About

You may also like...

Comments are closed.