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.

14 thoughts on “Lambda expression improvements

  1. Joshua Frank

    Thank you!

    If you can have multi-line functions, why not multi-line subs too? Those would be occasionally useful too, no?

    Reply
  2. Patrik Hägne

    Yes please! The lack of support for multi line lambdas is the feature I miss the most in VB at the moment. I think it’s absolutely crucial that this feature gets implemented to the next version or VB will "slip behind" too much since a lot of new frameworks really requires this to be useful (Moq and PLINQ for example). That subs must be supported to (the keyword might still be function though) goes without saying in my opinion.

    Reply
  3. Rory Becker

    Hell yeah! 🙂

    Could it support *optional* specification of functional return type. Personally I don’t need it but I know a few people who like to see this information

    Reply
  4. Jonathan Allen

    Patrik, I think we already have that.

    Dim y as Function(Of Integer, Integer) = Function(x) …

    Reply
  5. Patrik Hägne

    I’m not sure what you mean that we have Jonathan? I’m quite aware that we have lambda expressions, what we don’t have is lambda statements (what I called multi line lambdas). It is the lambda statements I mean are crucial.

    Reply
  6. Patrik Hägne

    I’m not sure what you mean that we have Jonathan? I’m quite aware that we have lambda expressions, what we don’t have is lambda statements (what I called multi line lambdas). It is the lambda statements I mean are crucial.

    Reply
  7. Pingback: ??????

  8. Matthew Doig

    How about defining lambda’s within our LINQ workflows? The following would be pretty cool.

    Dim result = (From n In Enumerable.Range(1, 999) _

    Dim multiple_of = Function(n, s) s.Any(Function(x) n Mod x = 0) _

    Where (n multiple_of([3;5;7;9]) _

    Select n).Sum()

    Reply
  9. alanJ

    Infering return types creates confusion.Don’t do it. Complex lambdas only good for devotees. VB aim and use was to be readable by all.The B in VB stands for Basic, lets keep it that way.
    alanJ

    Reply

Leave a Reply