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.
Thank you!
If you can have multi-line functions, why not multi-line subs too? Those would be occasionally useful too, no?
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.
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
Patrik, I think we already have that.
Dim y as Function(Of Integer, Integer) = Function(x) …
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.
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.
Joshua: We’d support multi-line Subs as well…
Pingback: ??????
It’s so good! I need it now!!!!!!!!!!!!!!!!!!!!!
I’m guessing from the response to Joshua we’ll be able to pass lamda expressions to methods expecting Action(of T)?
Any thoughts on using Sub vs Functions for this?
Here’s a quick explanation of the problem
http://blog.matthewdoig.com/?p=20
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()
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
Do it!
Lambda expressions are amazingly useful. The more flexible and powerful they can get the better.