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!

Please welcome Lucian Wischik!

Hi all, I wanted to take a moment to introduce the new specification lead for Visual Basic, Lucian Wischik. Lucian has been getting to know the user community over the past few months, and now you’ll have even more contact with him as he’ll be taking over my responsibilities in terms of owning the language spec! He’s already been an enormous help in ironing out some of the trickier aspects of the Dev10 spec, and I feel very confident that the specification will be in good hands. Here’s a short bio:

Lucian Wischik is the Visual Basic specification lead. Since joining the VB compiler team a year ago he has worked on new features for Visual Studio 2010 relating to type inference, lambdas and generic covariance. In his four years at Microsoft he has also worked on the Robotics SDK and concurrency, and has published several academic papers on the subject. Before coming to Microsoft he took a PhD in concurrency theory at the University of Cambridge and worked as a researcher at Bologna University in Italy. Despite this theoretical slant, he’s most at home when writing practical code. He’s a keen sailor and long-distance swimmer. His blog can be found at http://blogs.msdn.com/lucian.

Please welcome Lucian!

All good things…

As difficult as it is to say, I wanted to let my loyal readers know that after a decade spent working on Visual Basic, I’ve made the decision to change jobs at Microsoft.

It’s somewhat hard for even me to imagine just how long I’ve worked on Visual Basic. I joined the Developer Division (VB’s home) over 11.5 years ago to work on OLE Automation. A year and a half after that, I moved over to the VB team proper to work on the compiler’s code generator just as we started the move to what would become .NET. In some ways it seems like just yesterday, but in many other ways it feels like several lifetimes ago. In the intervening decade, I’ve worked on 4.1 versions of Visual Basic (including our forthcoming version), during which time an amazing amount of stuff has happened to the VB language, the VB community, Visual Studio, and development tools in general. The development world looks very different than it did when I started, and that’s generally been a wonderful thing.

But as the current release started to wind down from a design perspective, I started asking myself whether it was time for a change. And, after thinking about it quite a bit and talking to quite a few people, I decided that it was. As much fun as it has been to work on Visual Basic, I felt the need to be doing something different than what I’ve been doing. And so I made the decision that it was time to move on. Sort of.

You see, although the fact that I’m leaving is a big deal in some ways, it’s not as big a deal as you might expect. Even as I physically move to another team, in many ways I’m not really going anywhere. I’ll be carrying with me a title of “Visual Basic Language Designer Emeritus,” meaning that I will continue to participate in the VB language design process and will continue to work to ensure the VB language specification is kept complete and up to date (although I will no longer have primary authorship responsibilities). I’m also still planning to give the VB talk at the PDC in October and talking about all the exciting things we’ve done for the upcoming release plus some ideas about where things will be going in the more distant future.

As to my next challenge, well, there isn’t a whole lot I can say about that… yet. I’ve got some personal ideas rattling around in my head that I’m going to get some time to spend working on, but my day job is going to be working with guys like Douglas Purdy, Don Box and Chris Anderson on the Oslo product. In particular, I’m going to be helping out with the subject of this talk. Expect to hear more from me about it once we’ve gotten to the PDC.

Even though I’ll be spending a good bit of my time on Oslo, though, I’ll still going to be an active member of the VB community. I’ll still be talking about VB on this blog, opining on the language and it’s future, and, I’m sure, continuing to answer lots of questions. I’ll be continuing to use VB and am really excited where the product is going in this release and the next one (but more about that at the PDC!). Over time, I do expect my place in the VB community will fade somewhat as the new blood on the VB team really starts to come into its own, but for the time being things will continue to go along much as they have been. And, of course, VB will always have a special place in my heart.

This isn’t really goodbye but I did want to take the opportunity to thank all of the people out there who have used VB, who’ve read my blog, and who’ve written me or talked to me about VB. The VB community has been one of the major reasons that I have enjoyed working on VB so much (and for so long!), and every one of you have played a major role in that. There have certainly been controversies, disagreements, and blow ups, but I really think that VB has one of the finest user communities that I’ve ever participated in, and that I will be lucky if I work on products in the future that attract such a passionate, intelligent, and caring group of people. I can’t say how much I appreciate all the great times that everyone has given me over the years!

So, the blog will continue, I’ll still be talking about VB, and soon I’ll have some additional interesting and exciting things from my new job to talk about too. The next ten years should be just as fun as the previous ten have been!

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))
        {
            Console.WriteLine(i);
        }
    }
}

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)
            Console.WriteLine(i)
        Next
    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
            Else
                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.

Comments?

Reserved words: what are they good for? (Absolutely nothing?)

Random musing for the day: I was thinking about reserved words in programming languages and whether they’re really necessary at a lexical level. As you know, most programming languages define in their lexical grammar a set of words that cannot be used anywhere in the language except when explicitly specified in the grammar. For example, VB reserves the word “Object”. So you can’t just say:

    ' Error: Keyword is not valid as an identifier.
    Sub Object()
    End Sub

Many languages (such as VB) allow you to work around this by providing some sort of lexical escape that suppresses the reserved nature of the word. So you can say in VB:

    Sub [Object]()
    End Sub

Confusingly, many keywords that we’ve been adding to VB lately haven’t been reserved words, to reduce the need to modify people’s code when they upgrade. Instead, they’ve been contextual keywords, that is to say they’re only reserved in certain syntatic contexts. For example, From is not a reserved word in VB in the lexical grammar, but if you start an expression with From and then follow it with an identifier, we say “Oh, yes, you’re starting a query…” For example:

    Dim From As Integer = 10
    ' OK: Unambiguously the local variable
    Dim x = From + 10
    ' OK: Unambiguously a query
    Dim y = From a In New Integer() {1, 2, 3, 4}

Which leads me to wonder: why bother with lexically reserved words at all? Why not just make all of your keywords contextual? When I started on VB, I guess I just accepted the practice since that’s what the language did before I showed up, but now I’m not so sure. Maybe there’s some blindingly obvious reason that I’m not seeing (probably there is). I can think of some historical reasons why keywords weren’t all contextual:

  1. Maybe it simplified writing a parser in “the old days,” or it simplified building a parser generator.
  2. Maybe it was because people were writing code in editors that didn’t have syntax coloring. “int int = 5; int = (int)int * (int)int / (int)int” looks pretty nonsensical if you don’t have nice coloring to tell you which are the keywords and which aren’t.
  3. Maybe there were grammatical problems with doing it? The previous example makes me wonder about whether C could handle it; I’m not an expert on how the C grammar handles the cast operator.

Anyway, it’s not extremely relevant at the moment–we’re not going to just start unreserving all the keywords in VB–but just something interesting to think about…