您如何看待 VB 10 中的多行 lambda
我刚刚在 MSDN Channel 9 上观看了一个视频,可以找到 这里,介绍一下 Visual Basic 10 中的一些新功能。现在我喜欢大部分新功能,其中一些功能是我期待已久的(自动属性和集合)初始化器),引起我注意的是 C# 中的多行 lambda。
在视频中,他使用了这样的示例:
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)
现在我喜欢 VB 的所有冗长内容,但我只是有点担心编写 sub...end sub 内联可能会变得有点混乱,当您当您只需要使用 c => 之类的东西时,正在编写 C# {....} 并且您可以删除很多代码。
您对 VB 中的多行 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
就我个人而言,我认为 VB 的委托和 lambda 语法完全是假的。 我的意思是,来吧,
AddressOf
! 这在 VB6 中没问题。 在像 VB.NET 这样的语言中,这绝对是不好的,其中函数应该被视为一等公民(当然,尽管它们实际上不是),而且从方法组到委托的转换或多或少是透明的。现在内联函数的引入是非常冗长的。 我实际上相信 C# 方法 –
x => f(x)
在 VB 中表现得非常好,因为它准确地显示了它的作用。 在目前的状态下,我更喜欢 C# 来完成任何函数式编程工作,这很遗憾,因为我通常更喜欢 VB。现在,我真的很高兴 VB 终于获得了多行 lambda 和语句 lambda,因为它们有时仍然有用(以 Parallel.For 为例)。 但语法很混乱。 顺便说一句,迭代器也是如此(如果它们应该进入 VB10)。
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).我是一名 C# 开发人员,但现在几乎只使用 VB 9 大约一年了。 VB 9 让我心碎的第一件事就是有限的 lambda。 VB 9 中的 Lambda 受到以下限制:
因此,集合上的 ForEach 方法不适用于 lambda,只有最简单的操作才有效。 因此,大多数时候您必须将逻辑转移到其他方法并使用 AddressOf。 很多时候,这会以戏剧性和令人心碎的方式破坏代码的可读性。
我觉得很多人不会接受这一点,除非他们在另一种完全支持匿名方法的语言(C#、JavaScript 等)中流畅地使用匿名方法,而不是在 VB 9 中提供残缺的支持
。他们正在 VB 10 中修复 lambda,这让我松了口气。
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:
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.
我能立即想到两个我喜欢它的原因! 等这个已经太久了。
第一:
第二:
I can think of two reasons off the top of my head why I love it! Been waiting too long for this.
First:
Second:
同样,我喜欢 vb。 大多数时候你都在思考,而不是实际编写代码,所以在我看来,冗长的论点是失败的,因为你通常盯着代码或编辑它,想象一下当你在代码中阅读代码时,你可以节省理解代码的时间。 VB中的冗长? 与 C# 相比,更容易、更少出错和 bug。
另外,c# 仍然没有 with 子句,而 VB 甚至在 .net 时代之前就已经有了。
想象一下需要设置 10 项内容吗? 在 c# 中,您必须创建对 obj.class.methods 的引用并使用它进行速记表达,这会浪费内存并且效率低下,因此在这方面,vb 确实使用了更少的内存,并且您不会因为使用更少的内存而受到惩罚,这与C# 。
并且“using”关键字参数失败,因为 using 不适用于大多数对象或未实现 idisposable 的对象,这绝对令人烦恼。
然后,考虑在 C# 中(而不是在 vb 中)必须执行的所有显式转换。 C#errs 会争辩说,这鼓励更好的编码,但这是无稽之谈,因为任何优秀的开发人员都不需要每天显式地强制转换 500 次才能理解,如果他不这样做,就会发生隐式强制转换(就像在VB)。
大多数 c#errs 使用它是因为他们来自 ac 背景,这很好,但我发现很多人都是从它开始的,因为它包含字母 c,他们认为它更酷,因为它缺乏 VB 所具有的语言创新,使其成为对于开发人员来说更困难,这让他们感觉更聪明、更酷、高于其他人 - 哈哈,他们不明白以 0 成本隐藏复杂性是最终目标,而这正是 VB 可以为您做的事情。 请注意零成本部分,因为如果成本高于零,那将不是一件好事。
Same here, I love vb. Most of the time you are thinking and not actually writing code anyway, so the verbosity argument fails in my opinion, as you are usually staring at code or editing it, and imagine The time you are saving understanding your code when you read it in its verbosity in vb? Much easier and less error and bug prone as opposed to c#.
Also, c# still has no with clause, and vb has had this even prior to the .net days.
Imagine this with 10 things that need to be set? In c# you'd have to create a reference to obj.class.methods and use that for shorthand expressing, which is wasted memory and inefficient, so in that respect vb does use less memory and you are not punished for using less memory unlike with c# .
And the "using" keyword argument fails since using doesn't work with most objects or objects that don't implement idisposable which is absolutely annoying.
Then, think of all the explicit castings you have to do in c# as opposed to vb. C#errs will argue that is encourages better coding but that is nonsense, as any good developer doesn't need to explicitly cast something 500 times a day to understand that if he didn't an implicit casting would take place (as it does in vb).
Most c#errs use it because they come from a c background, which is fine, but I find a lot of them started with it because it contains the letter c and they think its cooler because it lacks the language innovation that vb has, making it harder for the developer, and that makes them feel smarter and cooler and above everyone else - lol, they don't understand that hiding complexity at 0 cost is the ultimate goal, which is what vb can do for you. Notice the at zero cost part, as it would not be a good thing if it was at above zero cost.
VB 中完整的匿名方法支持意味着您可以开始采用更实用的风格。 如果 Sub() End Sub 需要放在单独的行上......那就很痛苦了。 我希望他们允许单行匿名方法,只要只有一个语句即可。
Full anonymous method support in VB means you can start taking a more functional style with things. If the Sub() End Sub need to go on separate lines... that hurts. I'd hope they'd allow single-line anonymous methods, so long there was only one statement.
一旦我们获得 ParallelFX 库,您将需要多行。
例如,假设您想让这个循环并行:
并行版本将是:
它的工作原理是将循环的内部变成一个全新的子循环。 该新子进程由 N 个线程调用,N 通常是可用核心的数量。
You are going to need multi-line once we get the ParallelFX library.
For example, lets say you wanted to make this loop parallel:
The parallel version would be:
It works by turning the guts of the loop into a brand new sub. That new sub is called by N threads, N usually being the number of available cores.
没有简单的方法来管理此问题:
将 C# 语句主体 lambda 转换为 VB
没有多行 lambda。
叹气
所以是的,我很渴望这个能够完全发布。
There are no easy ways to manage this:
Convert C# statement body lambda to VB
without multiline lambdas.
sigh
So yes, I'm anxious for this to be fully released.