-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Meta-Contracts for Dynamics (For Tooling Improvements) #3012
Comments
Sounds like ducktyping, is overlapping with #2146 and #154 |
This is definitely something that we want to think about. |
Jon Skeet talked about this back in 2008: http://blogs.msmvps.com/jonskeet/2008/10/30/c-4-0-dynamic-lt-t-gt/ |
You aren't really casting here or anything like that, it almost seems like a specific case of pattern matching. I think the syntaxes should align (#206). |
I have been requesting that for years, with no luck. The only difference is that I proposed There's no need to complicate type specifications. If you need it to implement more that one interface, define one that extends those. |
I wonder how close could you get using just a library. For example, I could imagine something like: string fullName = GetFullName(new { FirstName = "John", LastName = "Doe" }.AsDuckInterface<IEntityWithName>()); where the extension method |
@svick there's one in case you're wondering ;) https://github.com/ekonbenefits/impromptu-interface |
No! I want dynamic with everything that comes with it, plus static typing. |
@paulomorgado ImpromptuInterface is based on |
We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages. |
Problem
Sometimes you are working with an object whose type you do not know, but know something about. For instance, I may be working with a
dynamic
object, but I may know that there will be aFirstName
andLastName
property. It would be nice to have a way to inform the tooling about the extra information that you know by declaring meta-contract information.Problem domain
Primary Objectives
Solution Setup
Lets say we have a function that concatenates first a last name. The function does not care what type is passed as long as it has two properties:
string FirstName
andstring LastName
.The consumer should feel free to pass any candidate type.
We could can achieve this today by making
entity
a type ofdynamic
, but we sacrifice tooling. We need a way to associate extra information withentity
.Solution
One solution would be to create an interface to serve as a contract and extend the syntax for
dynamic
to include the contract,dynamic<IMyContract>
.Other Considerations
dynamic
entity types. An improvement on this solution would be to support adding the contract to any type. For instance, I may accept entities of typeMyBaseType
that also conform to theIEntityWithName
contract.dynamic< IContractA & IContractB >
?dynamic< IContractA | IContractB >
?The text was updated successfully, but these errors were encountered: