Some background
I am very new to Swift and MacOS and trying to learn the Swift way to do things. This started because I realized that tuples are not hashable so I rolled my own:
struct XY: Hashable
{
let x: Int
let y: Int
init(_ tuple: (Int, Int))
{
x = tuple.0
y = tuple.1
}
}
This is well and good as now I can do stuff like make a Set of XY’s and manipulate tuples as a Set.
var xyset: Set<XY> = Set<XY>()
var xy1: XY = XY((3, 1))
var xy2: XY = XY((1, 1))
xyset.insert(xy1)
xyset.insert(xy2)
var found : Bool = xyset.contains(XY((1,1)))
print(found) //true
My issue
Having to XY everything is a bit redundant so I was looking to extend Set operations with my own functions so I can abstract some of that XY stuff out. I haven’t been able to find a good way to do this. I think what I want to do is roughly speaking something like this:
struct XYSet: Set<XY>
{
func insert(_ x: Int, _ y: Int)
{
this.insert(XY((x, y)))
}
func contains(_ x: Int, _ y: Int)
{
return this.contains(XY((x, y)))
}
}
I would like to be able to rewrite the above prior code could to:
var xyset: XYSet = XYSet()
xyset.insert(3, 1)
xyset.insert(1, 1)
var found : Bool = xyset.contains(1, 1)
print(found) //true