Category Archives: Visual Basic 2010

An embarrassment of riches on VB 10.0 and Oslo

Now that we’re past the PDC, there are a bunch of video resources coming out on VB 10.0 and Oslo. Here’s a roundup of what’s available so far:

The Pearson folks also recorded some vidcasts they call OnMicrosoft. If you go to the previous link, you can see all the videos posted, but the ones of interest to this blog are:

There are other Oslo vidcasts on the site, so check them out as well.

Future Directions for Visual Basic

Yesterday I gave my valedictory address on Visual Basic at the PDC. I think the talk went well and it was a lot of fun, if not a little sad that it’s one of the last times I’ll be giving a talk about Visual Basic. We covered a lot of exciting stuff, some of which should be familiar to readers of the blog. I’ll let people know when the video is up on the Channel9 page for the talk, should be some time today. For those of you who don’t want to sit through the talk it went something like this:

  • First, we talked a bit about the role of Visual Basic at Microsoft as the language that makes Microsoft platforms really accessible to programmers.
  • Then we segued into talking about the increased commitment that the languages groups are making to ensure that Visual Basic and C# coordinate language features so that users of one language aren’t left out in the cold when the other language adds some useful feature. This isn’t to say that we’re going to do things in exactly the same way, or even that the languages will have exactly the same feature set, but that we’re committing to ensuring that the fundamental capabilities in the languages stay in better sync than they have over the previous eight years.
  • Then Lucian did a really wonderful demo of VB 10.0, which is shipping in Visual Studio 2010. He showed (IIRC) the following features that should be familiar to the readers of this blog: array literals, collection initializers, automatic properties, implicit line continuations, statement lambdas, generic variance, and a feature that embeds primary interop assembly types in your assembly so you don’t have to deploy the PIA. I may have missed some, so check out the video when it’s posted!
  • Finally, we talked about some of the trends that we see affecting Visual Basic going forward and talked about some of the work we’re starting to do for post-VS 2010 to move the Visual Basic compiler to managed code and open it up to the world so that you can take advantage of the services that it provides.

If you attended the talk, please evaluate the session! It helps me become a better speaker and helps us give a better PDC. And feel free to stop by the tools lounge today, I’ll be hanging out there most of the day!

Where to Find Me at PDC2008

I didn’t realize I’ve been so silent about the PDC! I’ve been struggling to get off of .Text and on to Subtext for my blogging engine, and so I’ve been avoiding posting because “I’ll wait until I get moved over to the new engine.” Pffffft! Anyway, I’m going to be doing a number of things at the PDC, so if you’re going to be in LA next week, stop by and say hi!

  • I am co-presenting “Future Directions for Visual Basic” with Lucian on Tuesday at 5:15pm in room 406A. Do stop by and let’s chat about where Visual Basic is going! Also, feel free to go to the Channel9 page and add comments about what you’d like to see and if there are questions you’d like addressed. If you’re not going to the PDC, check the page after the talk for video and discussion.
  • I’ll be at the “Ask the Experts” dinner on Wednesday night in Hall G. I’ll be sitting with Visual Basic, although I might wander over to wherever Oslo is at some point.
  • I’ll be trying to attend the various Oslo talks.
  • I’ll be at the Tools and Languages lounge on Wednesday from 2-5pm for sure, and I’ll probably be hanging around there a lot otherwise.

I’ll be trying to keep up with my Twitter feed (my username is paulvick) so feel free to ping me if you are around. And I’ll blog some more after my talk.

Hope to see you all there!

Iterators in Visual Basic

WARNING: This is a speculative post. Caveat emptor.

Actually, in this case I don’t thing the above warning is strong enough. This is a super speculative post, because I believe the chance of it appearing in the next version of the language is not extremely high, not because it’s not a worthy feature but because it’s more than a little work and we’ve got a lot of other very worthy features we’re considering. However, since it’s something that’s valuable and something we keep getting requests for, we have decided to at least generally sketch out what iterators would look like in Visual Basic, with the idea that when the time comes that we have the resources to do them we can jump on it. Consider yourself double warned.

To start with, an iterator is just a structure that returns a sequence of values. Basically, when the IEnumerable(Of T) interface method GetEnumerator() returns an instance of the interface IEnumerator(Of T), that instance is an iterator. You can iterate through the collection by calling MoveNext on the enumerator and looking at the Current property until you’ve run out of values. Or you can use the For Each statement, which will do the iteration for you.

It’s possible to construct iterators in Visual Basic today–all you have to do is implement IEnumerable(Of T) and then implement IEnumerable(Of T). But the problem is that doing this involves a whole lot of boilerplate code that is the same in almost all iterators. On top of that, if an iterator involves a lot of conditions or branching, doing state management can be quite tricky. In an ideal world, what you’d like to be able to do is simply state the values that should be iterated over and have the compiler generate all the boilerplate code and state management for you. C# does this through the yield statement, which can be used in a method that returns an iterator to automatically generate the iterator:

using System;
using System.Collections.Generic;

class Program
    public static IEnumerable<int> FromTo(int low, int high)
        if (low <= high)
            yield return low;
            foreach (int i in FromTo(low + 1, high))
                yield return i;

    static void Main(string[] args)
        foreach (int i in FromTo(1, 5))

In this example, the FromTo method returns an iterator (typed as IEnumerable(Of Integer)) which is automatically generated by the compiler. What we’re considering in Visual Basic is essentially the same feature, but with a slightly different design. Instead of tying the iterator to a method, as in C#, we’re considering extending the multi-line lambda syntax that I talked about in the previous post to make “anonymous iterators.” The above example in VB would look something like:

Module Module1
    Function FromTo(ByVal low As Integer, ByVal high As Integer) As IEnumerable(Of Integer)
        Return Iterator
                   If low <= high Then
                       Return low
                       Return Each FromTo(low + 1, high)
                    End If
               End Iterator
    End Function

    Sub Main()
        For Each i In FromTo(1, 5)
    End Sub
End Module

There are a couple of differences to notice here:

  • An anonymous iterator is just an expression, so it can be used as the return value of a function but could also appear as the target of a For Each, used in a LINQ query, stored into a field, etc. (This is particularly useful when a function produces an iterator and you want to do some validation. In the C# model, the validation code is not run until MoveNext is first called on the iterator, not when the iterator is actually produced.)
  • By default the type of the iterator is IEnumerable(Of T), where T is inferred from all the types returned from the iterator using the same algorithm we use for multi-line lambdas. You could also explicitly state the iterator type using an As clause, and we would allow IEnumerator(Of T) as the iterator type just like C# does.
  • We don’t require the yield keyword since we’ve already marked the block as being special by using a new contextual keyword Iterator. (There would also be an Exit Iterator statement, not shown.)
  • Our plan would be to add an Each operator that “unwraps” a collection and returns each of its values one by one. This is generally useful but also addresses some performance issues with nested iterators which you can read more about in Wes Dyer’s post on iterators. (It might also later allow things like unwrapping collections in collection initializers, like {1, 2, 3, Each a, 5, 6, 7}, where a is an array of integers or something…)

We’re interested in people’s feedback on this proposed design and any other comments you might have.

Lambda expression improvements

WARNING: This is a speculative post. Caveat emptor.

I haven’t finished reading through all the comments from my previous post yet, but I did think it was worth stating that we are considering improvements to lambda expressions in the next version. Specifically, we’re looking at allowing single-line lambdas that don’t actually return anything, something like:

Dim x = Sub() Console.WriteLine(10)

This was something we wanted to support in 2008, but just ran out of time for. We’re also thinking about multi-line lambdas that contain statements instead of just a single expression. So something along the lines of:

Dim y = Function(x)
            If x > 0 Then
                Return x
                Return -x
            End If
        End Function

Like C#, we’d infer the return type of a multi-line lambda by attempting to pick the best type of all the Return statements.

This is really a common request, so it’s definitely something we’re strongly considering.

A little update on VB10 thinking…

WARNING: This is a speculative post. Caveat emptor.

It’s been a while since I’ve had much of anything to say about our thinking about VB10 (well, it’s been a while since I’ve had much of anything to say) and I wanted to give a quick update on our thinking:

We also keep getting asked about:

  • Iterators. This is definitely on our radar but it’s a big item. We’re considering it, but it hasn’t been thought about nearly as much as some of the other things above…

Another thing that is on our radar:

And, of course, other stuff that will be talked about in good time…

Collection initializer expressions, redux

WARNING: This is a speculative post. Caveat emptor.

When we last left collection initializers, we were discussing default types for collection initializers. Since then we’ve thought further about the feature and are considering changing the design. The problem is that as nice as type inference is, as we started to dig into what that practically meant it started to become more and more difficult to figure out what a particular collection initializer might mean. Because the meaning of the initializer in the original design depends on the context, it’s meaning could change dramatically in different contexts. Of particular difficulty was figuring out just how a collection initializer would participate in generic method type inference (i.e. inferring the type arguments to a generic method)–an important topic given how much generic methods are used in LINQ.

Without going into too much detail, what we’re considering is significantly simplifying the design. Instead of inferring the meaning of a collection initializer from context, a standalone collection initializer will always infer an array type. So {1, 2, 3, 4} will always be a single dimensional array of Integer. If you want to initialize a collection type, you use an initializer similar to object member initializers (the initializer that uses “With”). For example:

Dim x = New List(Of Integer)() From { 1, 2, 3, 4 }

The use of the “From” keyword is provisional. It may be the right keyword, it might not, we’ll have to think further on that one. However, you get the idea. The downside of the new design is that it’s more typing in the situation where you were initializing a collection in a context like argument passing (although in the case of List, you could also say {1, 2, 3, 4}.ToList()) where you weren’t going to say the type at all. The upside is that it removes significant ambiguity–when you see “Foo({1, 2, 3, 4})” you know what you’re passing and in the case of generic methods, you aren’t going to get surprising results. This also makes the feature work more like the way C#’s anonymous arrays and collection initializers work, for those of you who care about that kind of thing.


Collection initializer expressions

WARNING: This is a speculative post. Caveat emptor.

Well, I appear to be on a rhythm of about once a month posts, which seems OK for the moment. Moving on to another “future” topic, one of the most annoying things that we cut (at least, from my perspective) from VB 2008 was collection initializers. Collection initializers were a little different than the corresponding C# feature, because our plan was to introduce stand-alone initializers that didn’t have any intrinsic type. Instead the initializers “snap to” a particular type when they’re converted to it, just like lambdas and AddressOf expressions. For example:

' This works since VB 7.0!
Dim a() As Integer = {1, 2, 3, 4}
' This now creates a list.
Dim b() As List(Of Integer) = {1, 2, 3, 4}
' This assigns a new list to b.
b = {5, 6, 7, 8}

' If M takes Integer(), that's what it'll get. If it takes List(Of Integer), that's what it'll get.
M({1, 2, 3, 4, 5})

This is kind of nice, in that you can initialize collection types and arrays without having to state a type at all. We do the same trick C# does about looking for an Add method on a object that implements IEnumerable, so you can also use this syntax to initialize dictionaries:

Dim d As Dictionary(Of Integer, String) = {{1, "One"}, {2, "Two"}, {3, "Three"}}

All this is pretty straightforward, but there is some discussion about what type you should get if there is an absence of a target type. For example, assuming that local type inference is on, what should the following be typed as?

Dim x = {1, 2, 3, 4}

Our initial thought was that we should just infer the dominant type of the initializer and then create an array of that type. So the above example would type x as Integer(). And then the following:

Dim y = {{1, 2}, {3, 4}, {5, 6}}

Would infer a type of Integer(,), a two-dimensional array of Integer. With more nesting, you’d get more dimensional arrays. However, as we talked about it more, it seemed like maybe it might be more useful to infer List(Of T) for the 1-dimensional initalizer, Dictionary(Of K, V) for the 2-dimensional initializer, and nothing for more dimensions. So the type of x would instead be List(Of Integer) and the type of y would be Dictionary(Of Integer, Integer).

Both seem reasonable, so I’m curious what people would think. Remember, we will only fill in a type if we’re in a context (like type inference) where there’s no target type to use. We’ll always use a target type if there is one.

Implicitly implemented interfaces

WARNING: This is a speculative post. Caveat emptor.

This one is a little more speculative than the others, but it’s something that we’d like to get some feedback on. One complaint that we get from time to time has to do with interface implementation. A lot of people like the fact that there’s a nice explicit interface implementation syntax in VB, but sometimes it starts to feel, well, a little verbose. Even if you let the IDE create all the interface implementations for you, there’s still a lot of extra characters with all those “Implements IFoo.Bar”s, especially when the implementing method is the same name as the interface method.

One idea we’ve been kicking around is relaxing our rules to allow for implicit interface implementation. Today, when we’re checking interface implementation for some interface I1 what we do is:

  • Check to see if any base classes implement any of the members of I1. If so, we mark those methods as implemented.
  • Check to see if the current class implements any of the members of I1. If so, we mark those methods as implemented. If the current class implements a method that a base class implemented, the current class replaces that implementation.
  • If all of the methods of I1 are not implemented, we give an error.

What we would do is slightly modify the rules to:

  • Check to see if any base classes implement any of the members of I1. If so, we mark those methods as implemented.
  • Check to see if the current class implements any of the members of I1. If so, we mark those methods as implemented. If the current class implements a method that a base class implemented, the current class replaces that implementation.
  • If there are any methods of I1 that are not implemented, we check to see if there is a method with the same name and signature in the current class, and if there is, then that method implements the interface method.
  • If all of the methods of I1 are not implemented, we give an error.

What this would mean is that you could do something like this:

Class C1
    Implements IComparable(Of Integer)

    Public Function CompareTo(ByVal other As Integer) As Integer

    End Function
End Class

And not get any errors. There are a couple of things to note here if you’re familiar with C#, though. First, unlike in C#, if you declare a method in a derived class that has the same name as an interface method that is declared in a base class, you will still have to explicitly implement the interface method if you want the derived class method to take over. For example:

Class Derived
    Inherits Base
    Implements IComparable(Of Integer)

    ' Will not take over IComparable(Of Integer).CompareTo since Base already implements it!
    Public Function CompareTo(ByVal other As Integer) As Integer

    End Function
End Class

One could argue whether the derived class should or shouldn’t take over the implementation, but the bottom line is that if we allowed this we would be opening a potentially serious backwards compatibility problem. The above code could be written today (assuming Base implements IComparable(Of Integer)), and CompareTo will not take over the implementation of the interface. If recompiling your application in some future version of VB switched the implementation, this could introduce a very subtle and possibly very bad bug into your program. So it’s really a non-starter.

The other difference from C# is that we won’t search base classes for implicit interface implementations. For example, the following code would still give you an error:

Class Base
    Public Function CompareTo(ByVal other As Integer) As Integer

    End Function
End Class

Class Derived
    Inherits Base
    ' Error: IComparable(Of Integer).CompareTo is not implemented!
    Implements IComparable(Of Integer)
End Class

This can also be debated, but in the end we tend to err on the side of being more conservative-since Base didn’t implement IComparable(Of Integer) itself, it seems odd to pick it up when implementing the interface, as we have no way of knowing for sure that it’s really appropriate. There are also other highly technical issues relating to the mechanics of interface implementation that would make this situation complicated, especially if Base was in another assembly that we had no control over.

So the question is: would people find this useful? Annoying?

Automatically implemented properties

WARNING: This is a speculative post. Caveat emptor.

I apologize for the long silence, things have been a little busy around Panopticon Central these days! Anyway, I wanted to go back to talking about some of the things that we’re thinking about for the next version, and I thought I’d go over a small one: automatically implemented properties. As the name implies, these would be basically the same thing you get in C# 3.0’s auto-implemented properties.

What we’re talking about is allowing you to have a simple property declaration like so:

Public Property x() As Integer

This would generally expand under the covers to look something like this:

Private _x As Integer
Public Property x() As Integer
        Return _x
    End Get
    Set(ByVal value As Integer)
        _x = value
    End Set
End Property

The one major difference that we’re considering from C#’s auto-implemented properties is to allow initializers on the auto-implemented property. In other words, something like:

Public Property x As Integer = 10
Public Property y As New List(Of Integer)()

I think C# also doesn’t let you bind to the underlying field, but just like with other auto-generated stuff in VB, I think we’d err on the side of allowing you to bind to it if you really needed to. You can also make them Overridable if you like, to allow derived classes to provide their own implementation.

Just a little feature, but one that can be useful.