To support the query features supported by the Linq platform, VB9 must introduce a small number of functional programming (FP) features. The nested functions and Closue features introduced in this article are the most important part. Before discussing why such features are introduced, let's take a look at the features. VB9 allows
Define functions or subprograms in the processSuch:
Public Sub MyProc()Sub MyProc()Sub Nested()Sub Nested()'Code here End SubEnd Sub
The first purpose that can be considered is that a helper function can be used only for a specific process, and nested functions defined in this process cannot be accessed outside the process. This is similar to nested functions in Delphi. Note that nested functions can only be accessed in the context after definition. The following is the biggest difference between nested functions and traditional functions:
Public Sub MyProc()Sub MyProc()Dim x As IntegerSub Nested()Sub Nested(i As Integer) x = iEnd Sub'Invoke subroutine Nested(20) Console.WriteLine(x);End Sub
Nested functions can access variables in external processes. This feature is calledClosure. With Closure, we can generate functions with variable rules to achieve latency computing. This is a basic function of FP. Closure, an embedded function of VB9, may be implemented by using the Tuple feature. However, there is no further computing on the principle. Compared with C # anonymous functions, VB9 embedded functions have two main advantages:
1. It can be called repeatedly without being delegated. You can obtain it through AddressOf when you need to delegate.
2. Direct recursion is supported. (Of course, the master can use the Y operator. Let's talk about it later ......)
Visual Basic also considers adding the expression of anonymous embedded functions, which is well known.Lambda expressions. However, no syntax is provided, so I will not introduce it first.