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.
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: