Geeks With Blogs
Arnold Awulu Lartey

I found a whitepaper which presented lastest developments in VB10 the one yet to be release this year. Now VB supports multiline lambdas,sub lambda and single-line lambdas. Now we have expressions like

Dim lambda = Function(n As Integer) As Single

                     If n Mod 2 = 0 Then

                         Return 3

                     Else

                         Return 3.14

                     End If

                 End Function

 



Another feature is the implicit line continuation. So now a statement like the following is valid.

    <Attribute()>

    Function Go(

                    ByVal x As Integer,

                    ByVal y As Integer,

                    ByVal z As Integer

                )

        Dim query =

            From n In {

                       123,

                       456,

                       789

                      }

            Order By n

            Select n +

                   x

 

    End Function

 

Collection Initializers

Collection initializers provide a way to initialize a collection type and provide it with a series of default values within a single expression.  For example:

    Dim list = New List(Of String) From {"abc", "def", "ghi"}

 

Each element after the “From” keyword will be passed to the Add method on the type:   

    list.Add("abc")

    list.Add("def")

    list.Add("ghi")

 

Similarly, Dictionaries can also be initialized this way:

    Dim list As New Dictionary(Of Integer, String) From

                                                        {{1, "abc"},

                                                         {2, "def"}}

When the elements after “From” are nested, the compiler will “unpack” the elements inside each set of brace and pass them sequentially to a single Add call:   

        list.Add(1, "abc")

        list.Add(2, "def")

 

Developers are free to provide their own implementation of Add, either as an instance method on the type or through an extension method:

    Dim list As New List(Of Customer) From {

                                            {123, "Jonathan", "Aneja"},

                                            {456, "Lucian", "Wischik"},

                                            {789, "Paul", "Vick"}

                                           }

 

    Class Customer

        Property ID As Integer

        Property FirstName As String

        Property LastName As String

    End Class

 

    <Extension()>

    Sub Add(list As List(Of Customer),

            ID As Integer,

            FirstName As String,

            LastName As String)

 

        list.Add(New Customer With {

                                        .ID=ID,

                                        .FirstName=FirstName,

                                        .LastName=LastName

                                   }

    End Sub

 

Array Literals

Array literals provide a compact syntax for declaring an array whose type is inferred by the compiler.

    Dim a = {1, 2, 3} 'infers Integer()

    Dim b = {1, 2, 3.5} 'infers Double()

    Dim c = {"1", "2", "3"} 'infers String()

    Dim d = {1, "123"} 'infers Object() (warning with Option Strict On)

 

Nested array literals can be used to produce multidimensional arrays:

    Dim e = {{1, 2, 3}, {4, 5, 6}} 'infers Integer(,)

    Dim f = {({1, 2, 3}), ({4, 5, 6})} 'infers Integer()() (jagged array)

 

Nullable Optional Parameters

Optional parameters can now be typed as Nullable and initialized to a default value:

    Sub Add(x As Integer, y As Integer, Optional z As Integer? = Nothing)

 

    Sub Add(x As Integer, y As Integer, Optional z As Double? = 4)

 

Generic Variance

(VB and C# are introducing this feature simultaneously, and the implementations are practically identical.  The following section is adapted from Mads Torgersen’s excellent write-up in the equivalent document for C#.)

An aspect of generics that often comes across as surprising is that the following is illegal:

    Dim strings As IList(Of String) = New List(Of String)

    Dim objects As IList(Of Object) = strings

 

The second assignment is disallowed because strings does not have the same element type as objects. There is a perfectly good reason for this. If it were allowed you could write:

    objects(0) = 5

    Dim s As String = strings(0)

 

This would be allowing an Integer to be inserted into a list of Strings and subsequently extracted as a String, which would be a breach of type safety.

However, there are certain interfaces where the above cannot occur, notably where there is no way to insert an object into the collection.  One such interface is IEnumerable(Of T).  If instead you say:

    Dim objects As IEnumerable(Of Object) = strings

 

There is no way we can put the wrong kind of thing into strings through objects, because objects does not have a method that takes an element in.  Variance is about allowing assignments such as this in cases where it is safe. The result is that a lot of situations that were previously surprising now just work.

Covariance

In .NET 4.0 the IEnumerable(Of T) interface will be declared in the following way:

    Interface IEnumerable(Of Out T) : Inherits IEnumerable

        Function GetEnumerator() As IEnumerator(Of T)

    End Interface

 

    Interface IEnumerator(Of Out T) : Inherits IEnumerator

        Function MoveNext() As Boolean

        ReadOnly Property Current() As T

    End Interface


Well for more, check out these resources.

 Visual Basic Team Blog (http://blogs.msdn.com/vbteam) and the

Visual Basic Developer Center (http://msdn.com/vbasic).  

VB10  whitepaper is available at the Code Gallery site http://code.msdn.com/vbfuture

Have fun with Visual Basic 10!

 

Posted on Tuesday, January 5, 2010 12:10 AM | Back to top


Comments on this post: Visual Basic 10

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © arnoldine | Powered by: GeeksWithBlogs.net