Swift development sixth-operator operators can also overload & func parameter Decoration

Source: Internet
Author: User

This article is divided into two parts:

1. Use of overloaded operator operators in Swift 2, parameter decoration of func in Swfit

1. Use of overloaded operator operators in Swift

Unlike other languages, Swift supports overloading of operators, which refer to operator operators such as "+,-,%,*", making some operations easier.

In development, we usually write about some of the basic operations:

struct vector2d {    0.0 0.0 2  3      14= vector2d (x:v1.x+v2.x, Y:V1.Y+V2.Y)

The output of the V3 is: 3,7

However, when this two-bit array is too large, if we still use this way, our code will become abnormally bloated, the programmer will become impatient, so Swift in order to improve efficiency, the creation of overloaded operators this kind of literary programming, the above code can be optimized to:

// Swift supports features such as overloaded operators, making some calculations easier func + (left:vector2d, right:vector2d)- vector2d {      Return vector2d (x:left.x + right.x, Y:left.y += v1 + v2 + v3  //  continue to add both

The output of V is: 6,14

Because the +,-, * This operator is already defined in Swift, so it's not a mistake, and if we're going to add a new operator, we need to declare it first.

operator +* {  ///  infix: Indicates that a bitwise operator is to be defined, that is, both front and back input, and other adornments include prefix and postfix    associativity None  //  associativity: Define the Binding law, specify the order of calculation, add subtraction to left, point multiply with none        // Precedence: Priority of the operation, the priority of multiplication in Swift is 150, plus minus is }
Func +* (left:vector2d, right:vector2d), Double {    return left.x * right.x + left.y * /c17>= v1 +* v2

The output of the RESULT1 is: 14.0

Note: The Swift operator is not defined in the local domain, because at least you will want to be able to use your operator at the global scope, otherwise the operator loses its meaning.

2. Parameter modification of func in Swfit

All variables in Swift are immutable by default, that is, with let, which is the same as in the method, so this code will error: Func test (i:int), Int

Workaround:

Func Incrementor (Var i:int), Int {    return + +i}print (" The result of the calculation is: \ ( Incrementor (7))")

Output: The calculated result is: 8

// The combination of curry and multivariate groups, the inout modifier used the func makeincrementor (Addnumber:int), ((InOut Int), ( ))     in a multivariate group { () {        + = addnumber;        print (variable);    }     return  2= Makeincrementor (3) addmethod (&i) Addmethod ( &i) addmethod (&i) addmethod (&i)

Swift development sixth-operator operators can also overload & func parameter Decoration

Related Article

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.