从函数返回(带有函数的匿名类型) - 有更好的方法吗?
众所周知,这个问题主要是学术性的,尽管我尝试在现实世界的解决方案中使用这个概念。我意识到这个例子是人为的,但我相信这个概念是有效的。
我想编写一些像这样流畅的代码:
copy(my_first_file).to(my_second_file)
简短,易于阅读和理解,完全合法。所以我这样定义我的复制方法:
Private Function copy(FilePath as String) As Object
Return New With {.to = New Action(Of String)(Function(x) my_real_copy_method(FilePath,x))}
End Function
我意识到我不能将匿名类型强制转换为特定类型(例如实现接口或其他一些类),并且我不希望定义特定类的开销只是为了将我想要的流畅名称与实际方法名称相匹配。所以我能够使代码像这样工作:
copy(my_first_file).to.Invoke(my_second_file)
所以那里没有 IntelliSense 或类型感知,我必须包含 Invoke 才能运行该方法。在这些约束下,如何获得更多类型安全性并排除 Invoke 方法:
- 从方法返回的匿名类型
- 没有其他类或接口
- 最好,我不想将另一个参数传递给 copy() 方法来告诉返回什么类型,除非 copy 成为通用方法(但我认为这意味着定义另一个类/接口,我不想这样做)
我知道这听起来相当要求,如果我太难了,请随意打电话给“Bull”!
提前致谢。
Just so it's known, this question is mostly academic, even though I tried to use the concept in a real-world solution. I realize the example is contrived, but I believe the concept is valid.
I want to write some fluent code like this:
copy(my_first_file).to(my_second_file)
Short, easy to read and understand, perfectly legit. So I define my copy method like this:
Private Function copy(FilePath as String) As Object
Return New With {.to = New Action(Of String)(Function(x) my_real_copy_method(FilePath,x))}
End Function
I realize that I can't force an anonymous type into a specific type (like implementing an interface or some other class), and I don't want the overhead of defining a specific class just to match my desired fluent name with the actual method name. So I was able to make the code work like this:
copy(my_first_file).to.Invoke(my_second_file)
So there is no IntelliSense or type awareness there, and I have to include the Invoke in order to have the method run. How can I get more type safety and exclude the Invoke method, under these constraints:
- Anonymous Type returned from Method
- No additional classes or interfaces
- Preferably, I do not want to pass in another parameter to the copy() method that tells what type to return, unless copy becomes a generic method (but I think that means defining another class/interface, which I don't want to do)
I know that sounds pretty demanding, feel free to call "Bull" if I'm being to difficult!
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道你说你的例子是人为的,但是这个怎么样?它支持方法链接(
CopyTo
返回FileInfo
),即使它不是流畅。它也内置于 System.IO 中。I know you said your example is contrived, but how about this? It supports method chaining (
CopyTo
returns aFileInfo
), even if it's not fluent. It's built intoSystem.IO
too.由于 VB 没有泛型方法的返回类型推断,即使没有歧义,也没有办法做到这一点。
您可以拥有一个使用泛型返回匿名类型的强类型函数,但不能使用推断泛型调用它,您需要显式指定它们。
(适应.NET的命名约定)
必须按如下方式调用:
Since VB has no return type inference for generic methods, even where there is no ambiguity, there is no way of doing this.
You can have a strongly typed function that returns an anonymous type using generics but you cannot call it with inferred generics, you need to specify them explicitly.
(Naming convention adapted to .NET)
This must be called as follows:
对不起;这是我能想到的最好的办法。它不满足您的要求......但它确实为您提供了您想要的语法。
I'm sorry; this is the best I could come up with. It doesn't meet your requirements....but it does give you the syntax you wanted.