从函数返回(带有函数的匿名类型) - 有更好的方法吗?

发布于 2024-10-20 10:43:24 字数 842 浏览 2 评论 0原文

众所周知,这个问题主要是学术性的,尽管我尝试在现实世界的解决方案中使用这个概念。我意识到这个例子是人为的,但我相信这个概念是有效的。

我想编写一些像这样流畅的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你手 2024-10-27 10:43:25

我知道你说你的例子是人为的,但是这个怎么样?它支持方法链接(CopyTo 返回 FileInfo),即使它不是流畅。它也内置于 System.IO 中。

Dim f As New FileInfo("c:\x.txt")
f.CopyTo("c:\y.txt")

I know you said your example is contrived, but how about this? It supports method chaining (CopyTo returns a FileInfo), even if it's not fluent. It's built into System.IO too.

Dim f As New FileInfo("c:\x.txt")
f.CopyTo("c:\y.txt")
生寂 2024-10-27 10:43:24

由于 VB 没有泛型方法的返回类型推断,即使没有歧义,也没有办法做到这一点。

您可以拥有一个使用泛型返回匿名类型的强类型函数,但不能使用推断泛型调用它,您需要显式指定它们。

Private Function Copy(Of T)(filePath as String, prototype As T) As T
    Return New With { .To = … }
End Function

(适应.NET的命名约定)

必须按如下方式调用:

Dim nullAction As Action(Of String) = Nothing
Dim proto = New With { .To = nullAction }
Copy(firstFile, proto).To(secondFile)

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.

Private Function Copy(Of T)(filePath as String, prototype As T) As T
    Return New With { .To = … }
End Function

(Naming convention adapted to .NET)

This must be called as follows:

Dim nullAction As Action(Of String) = Nothing
Dim proto = New With { .To = nullAction }
Copy(firstFile, proto).To(secondFile)
酒儿 2024-10-27 10:43:24

对不起;这是我能想到的最好的办法。它不满足您的要求......但它确实为您提供了您想要的语法。

Sub Main()
    copy("myFile").To("myOtherFile")
End Sub

Private Function copy(ByVal FilePath As String) As String
    Return FilePath
End Function

<Extension()> _
Public Sub [To](ByVal FromFile As String, ByVal ToFile As String)
    ' Code to really copy goes here
    my_real_copy_method(FromFile, ToFile)
End Sub

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.

Sub Main()
    copy("myFile").To("myOtherFile")
End Sub

Private Function copy(ByVal FilePath As String) As String
    Return FilePath
End Function

<Extension()> _
Public Sub [To](ByVal FromFile As String, ByVal ToFile As String)
    ' Code to really copy goes here
    my_real_copy_method(FromFile, ToFile)
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文