I have the following structs that represent a point or a line:
public struct Point{
let x : Double
let y : Double
init (x : Double, y : Double)
{
self.x = x
self.y = y
}
}
extension Point : Equatable{}
public func ==(lhs: Point, rhs: Point) -> Bool
{
return lhs.x == rhs.x && lhs.y == rhs.y
}
And
public struct Line {
let points : [Point]
init(points : [Point])
{
self.points = points
}
}
extension Line : Equatable {}
public func ==(lhs: Line, rhs: Line) -> Bool
{
return lhs.points == rhs.points
}
I want to be able to have a Shape protocol or struct that I can use to have Points and Lines and then I can compare between them. I tried to that with conforming protocol Shape but Swift compiler gives me an error when I want to compare a point with a line even though they are Shapes.
Do I have to move from struct to classes?
I think I may have to use generics but don't know exactly how to solve this issue.
Thanks in advance for any guidance.
Edit1:
My approach to Shape protocol was really just trying stuff but nothing worked. I tried the following:
protocol MapShape : Equatable
{
func == (lhs: MapShape, rhs: MapShape ) -> Bool
}
I also changed the code for the Equatable extension for lines given the suggestion