The short-circuit evaluation of the conditional expression and the delay evaluation of the function, and the expression evaluation

Source: Internet
Author: User

The short-circuit evaluation of the conditional expression and the delay evaluation of the function, and the expression evaluation

The latency is. A very important feature of. NET. In the LISP language, this feature relies on macros. In C, C ++, function pointers can be used. it is done by delegation. If you don't understand what latency is, let's take a look at the following code:

      static void TestDelayFunction()        {            TestDelayFunton1(true,trueFun3);        }        static void TestDelayFunton1(bool flag , Func<bool> fun  )        {            if(flag)               fun();        }

In method TestDelayFunton1, whether the function-type parameter fun is evaluated depends on the first parameter flag. If its value is false, function fun will never be evaluated, here, the evaluate of the function fun is postponed to the inner of the TestDelayFunton1 method, rather than during parameter calculation.

Latency evaluation is very useful. It can avoid unnecessary computation. For example, in the above example, this can save computing costs, if fun is time-consuming.

Note the following code:

if(flag)   fun();

 

In fact, it is equivalent to a logical expression:

bool result= flag && fun();

In this expression, whether the fun () function is evaluated depends on the variable flag. This function is called short circuit judgment, the "Conditional short circuit" function implements our "latency evaluation" function. Therefore, we can draw the following inferences:

At any time, if a function fun needs to evaluate the latency, it can be expressed as a conditional expression:

(Test() && fun())

Therefore, the first two functions can be rewritten to the following function in essence:

      static void TestDelayFunton2(bool flag)        {            bool result = flag && trueFun3();        }

It calls TestDelayFunton1 (true, trueFun3); and converts it into the preceding function call.

Of course, to make this call available, we still need to solve the problem that the function fun () type is not the bool type. This problem is easy to handle. Just wrap the function:

bool WarpFunction(){  fun();  return true;}

The subsequent calls will look like this:

(Test() && WarpFunction())

In this example, it is equivalent:

(flag && trueFun3())

 

If it is a "smart" compiler, it can complete the above conversion. The following provides a complete code image so that you can see it more clearly:

The two functions marked above are equivalent to the following function. That is to say, the call of TestDelayFunton1 is transformed into the call of testdelayfunton2.

 

If you still don't understand the above process, let's look at the example below:

 static bool trueFun1()        {            Console.WriteLine("call fun 1");            return true;        }        static bool falseFun2()        {            Console.WriteLine("call fun 2");            return false;        }        static bool trueFun3()        {            Console.WriteLine("call fun 3");            return true;        }

Will trueFun3 be executed when the following code is executed?

if (trueFun1() && falseFun2() && (trueFun3())){             } Console.WriteLine();if (trueFun1() || falseFun2() || trueFun3()){}

If you fully understand C #'s "Conditional short circuit" feature, I believe the answer will soon come out.

 

After reading this article, you may askOdd and clever, What is the role?

If you study it in depth. NET delegate, you will understand that the delegate call is actually to wrap a function with an object ,. NET automatically generates a lot of code for you, and the performance will inevitably suffer. If you need code with the best performance in some places, this technique will certainly help you, if you can write a compiler for this type of conversion, congratulations! The future is yours!

 

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.