Swift operator functions and custom operators

Source: Internet
Author: User

Code:

structvector2d {var x=0.0, y =0.0}//Vector1 + Vector2Func + (left:vector2d, right:vector2d)vector2d {returnVector2d (X:left.x + right.x, Y:left.y +right.y)}//Vector1-vector2Func-(left:vector2d, right:vector2d)vector2d {returnVector2d (X:left.x-right.x, Y:left.y-right.y)}//-vectorPrefix func-(vector:vector2d)vector2d {returnVECTOR2D (x:-vector.x, y:-vector.y)}//Vector1 + = Vector2Func + =(InOut left:vector2d, right:vector2d) { left= left +Right }//Vector1-= Vector2Func-=(InOut left:vector2d, right:vector2d) { left= left-Right }//++vector1Prefix func + + (inout v:vector2d)vector2d {v+ = vector2d (x:1Y:1)    returnv}//vector1++Postfix func + + (inout v:vector2d)vector2d {v+ = vector2d (x:1Y:1)    returnv}//--vectorPrefix func--(InOut v:vector2d)vector2d {v+ = vector2d (x:1Y:1)    returnv}//vector--Postfix func--(InOut v:vector2d)vector2d {v+ = vector2d (x:1Y:1)    returnv}//Vector1 = = Vector2Func = = (left:vector2d, right:vector2d)Bool {return(Left.x = = right.x) && (Left.y = =right.y)}//Vector1! = Vector2Func! = (left:vector2d, right:vector2d)Bool {return! (left = =Right )}//+++vectorPrefixoperator+++{}prefix func+ + + (inout vector:vector2d)vector2d {vector+=VectorreturnVector}//vector+++Postfixoperator+++{}postfix func+ + + (inout vector:vector2d)vector2d {vector+=VectorreturnVector}//Vector1 +-Vector2Infixoperator+-{associativity left precedence $}func+-(left:vector2d, right:vector2d)vector2d {returnVector2d (X:left.x + right.x, Y:left.y-right.y)}//Vector1-+ Vector2Infixoperator-+ {associativity Left precedence $}func-+ (left:vector2d, right:vector2d)vector2d {returnVector2d (x:left.x-right.x, Y:left.y +right.y)}

Swift operator functions and custom operators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.