Different implementations for the same function

I want to intersect two Surfaces of the Protocol surfaces. Some Objects like a plane implement this protocol. Normally I want to call a general function, but to solve the intersection faster between to Planes I want to call a different function. I could make a master func wich Type checks and then calls the right functions. But I am sure there is a better way, where the complete does this for me. I would be much more readable.

func intersect(a: Surface, b: Surface) -> Curve3D {
// general implementation
 }

func intersect(a: Plane, b: Plane) -> Curve3D {
// special implementation
 }

I tried this, but does not work. When I mark the varsity as Surface but they are still plane Strukts and then intersect them the general implement is chosen.

I hope everything is clear. Thanks for the Help 🙏🏻

Where and how are Surface and Plane defined ?

Could you post a sample code to reproduce ?

Additional Information:

protocol Surface {
//Some general  stuff for the intersection (not implemented yet)
}

struct Plane: Surface {
var point: SIMD3
var normal: SIMD3
}

struct Zylinder: Surface {
var point: SIMD3
var vector: SIMD3
var d: Double
}

struct Customsurface: Surface {
//not implement yet
//You will be able to creat any shap you want
}

So I want a function which has more implementations, so I can short cut a plane plane intersection. This functionality should still be available, when I intersect two items of [Surface]. (Zylinder is just an additional Type which is very common.)

I hope it is more clear now.

I did a rapid test (in Playground) and got the expected result (I changed some types to simplify):

protocol Surface {
    //Some general  stuff for the intersection (not implemented yet)
}

struct Plane: Surface {
  var point: Int
  var normal: Int
}

struct Zylinder: Surface {
  var point: Int
  var vector: Int
  var d: Double
}

struct Customsurface: Surface {
  // not implement yet
  // You will be able to create any shape you want
}


func f(a: Plane) {
    print("Plane")
}

func f(a: Zylinder) {
    print("Zylinder")
}

let pl = Plane(point: 10, normal: 10)
let zy = Zylinder(point: 100, vector: 100, d: 100.0)

f(a: pl)
f(a: zy)

Result:

Plane
Zylinder

So, what do you get, what did you expect ?

You do not show how you call

intersect(a: Surface, b: Surface)

What a and b parameter values do you pass to intersect ?

Additional Information 2:

That‘s how I want to call it.

protocol Surface {
    //Some general  stuff for the intersection (not implemented yet)
}

struct Plane: Surface {
  var point: Int
  var normal: Int
}

struct Zylinder: Surface {
  var point: Int
  var vector: Int
  var d: Double
}

struct Customsurface: Surface {
  // not implement yet
  // You will be able to create any shape you want
}

func intersect(a: Surface, b: Surface)  {
print(“custom“)
 }

func intersect(a: Plane, b: Plane)  {
print(“plane“)
 }

let surface = Plane(point: 10, normal: 10)
let array: [Surface] = [Plane(point: 10, normal: 10), Costumsurface()]

for i in array {
intersect(a: surface, b: i)
}

Wanted results:

plane
general

Got results:

genaral
general

I hope this make my goal more clear.

Different implementations for the same function
 
 
Q