Operator Overloading

This topic has been discussed at the following article

Modelisation of basic Sets theory by Swift


Notice :
Scope of operator overloading in Swift is global ,let's suppose  in our ViewController:

 

struct myVector{

    var x:Int

    var y:Int

}

func + (left:myVector,right:myVector)->myVector{

    

    return myVector(x:left.x+right.x , y:left.y+right.y)

}

class ViewController: UIViewController

 

2.prefix,infix,postfix

When overloading prefix you need to declare operator before

 

prefix operator +++{}//declare operator

prefix func +++ (x:Int)->Int{

    return (x+x+x)

}


 

 © Xosrov 2016