Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you think of multiline lambdas in VB 10

Tags:

vb.net

lambda

I was just watching a video on MSDN Channel 9 which can be found here, about some of the new features in Visual Basic 10. Now I like most of the new features, some of which have been long awaited(auto properties and Collection Initializers), one that caught my eye was the multiline lambdas like in C#.

In the video he used an example like this:

Dim scores = {10,20,30,40,50}
Dim thread as new Threading.Thread(Sub()
                                   For Each o in scores
                                     console.writeline(o)
                                     Next
                                   End Sub)

Now I like VB in all it verbosity but I'm just a bit worried that writing sub...end sub inline could get a bit messy, I can see some merit in inlining when you are writing C# when you only have to use something like c => {....} and you can cut out a lot of code.

What are your throughts of multiline lambdas in VB?

Would you find them useful and where?

like image 383
Nathan W Avatar asked Oct 30 '08 04:10

Nathan W


2 Answers

Personally, I think that VB's syntax for delegates and lambdas is completely bogus. I mean, come on, AddressOf! This was fine in VB6. It is definitely not fine in a language such as VB.NET where functions should be treated as first-class citizens (although they really aren't, of course) and where conversion from method groups to delegates is more or less transparent.

Now the introduction of inline functions is horribly verbose. I actually believe that the C# approach – x => f(x) would fare very well in VB because it shows exactly what it does. At the current state, I prefer C# for any functional programming work, which is a pity because I generally favour VB.

Now, I really rejoice that VB finally gets multiline lambdas and statement lambdas because they're still useful sometimes (take the case of Parallel.For). But the syntax is messed up. The same goes for iterators, by the way (if they should make it into VB10).

like image 176
Konrad Rudolph Avatar answered Nov 22 '22 05:11

Konrad Rudolph


By preference I'm a C# developer, but have been using VB 9 almost exclusively for about a year now. The #1 thing about VB 9 that breaks my heart is the limited lambdas. Lambdas in VB 9 are limited in the following ways:

  • Only one statement.
  • They must return a value.

So the ForEach method on collections will not work with lambdas, and only the very simplest of operations will work. So most of the time you have to move your logic to some other method and use AddressOf. Many times this cleaves the readability of the code in a dramatic and heartbreaking way.

It's something that I feel many would not pick up on unless they've used anonymous methods fluently in another language that fully supports them (C#, JavaScript, etc.), rather than the crippled support they have in VB 9.

I'm extremely relieved that they're fixing lambdas in VB 10.

like image 27
pettys Avatar answered Nov 22 '22 05:11

pettys