Source Code analysis in swift-@autoclosure

Source: Internet
Author: User

@autoclosure: The literal meaning is to automatically close the packet.

There are such operators in Swift && we know that the && operator is blocked

In Swift, the operator is a function, and if && on the left is false, the right side is not computed and returns false directly.

@inline (__always) @warn_unused_result  public func && <t:boolean, u:boolean>(   Bool {   C7>returnTryfalse}
@_transparent@warn_unused_result  public func && <T:Boolean>(   Bool {  return)  tryfalse}

From the source code, we can see that the left parameter is the normal Boolean, the right argument is not a Boolean, but a closure with @autoclosure decoration.
(Can still throw an exception)

Why is this? The following is a hint before the function, which is very well written, given an example, because when the first argument is false there is no need to calculate the second argument. Because it must be false.

///performs a logical AND operation on the Boolean values.//////The ' && ' (logical AND) operator combines the Boolean values and returns///' true ' if both of the values is ' true '. If either of the values is///' false ', the operator returns ' false './/////This operator uses short-circuit evaluation:the left-hand side (' LHS ') is///evaluated first, and the right-hand side (' RHS ') are evaluated only if///' LHS ' evaluates to ' true '. For example://////Let measurements = [7.44, 6.51, 4.74, 5.88, 6.27 , 6.12, 7.76]///Let sum = measurements.reduce (0, combine: +)//////if measurements.count > 0 && sum/double (measurements.count)< 6.5 {///print ("Average measurement is less than 6.5")///     }/////Prints "Average measurement is less than 6.5"//////in this example, ' LHS ' tests whether ' measurements.count ' is greater than///Zero. Evaluation of the ' && ' operator is one of the following://////-When ' measurements.count ' was equal to zero, ' LHS ' evaluates to ' false '///and ' RHS ' isn't evaluated, preventing a Divide-by-zero error in the///expression ' sum/double (Measurements.count) '. The result of the///operation is ' false './//-When ' measurements.count ' was greater than zero, ' LHS ' evaluates to ' true '///and ' RHS ' is evaluated. The result of evaluating ' RHS ' is the result of///The ' && ' operation.

So why use closures?

Let's raise a chestnut.

simulate the && operator: reflect quickly, and soon the results

But we don't use it that way when we use it.

Then you'll see that the result is waiting for about five seconds, but the result is still false.

The first parameter is already false, and the second result is meaningless, but the simple compiler will call a () to calculate the value of the returned result, and then judge it. A lot of time and resources have been wasted.

Improved: In the form of closures:

The results will soon be displayed. Because the closure is not executed immediately, the closure is executed only when the first argument is true.

But when you call this function, it is inconvenient to use {xxxx}.

Further improvement: using @autoclosure Modification

This is not very clear.

@autoclosure can only be used in ()->t such as a non-parametric closure

Source Code analysis in swift-@autoclosure

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.