Structural Typing keyword and associated compiler checks (Structural Typing Yet Again...) #9284
Unanswered
davidwhitney
asked this question in
Language Ideas
Replies: 2 comments 3 replies
-
I expect this kind of functionality will come about through the exploration into extension implementation. You would still have to declare that extension, but it's possible that the compiler could automatically wire up all of the required members if they already exist or could be satisfied through other extensions. Some members of the team have expressed a preference in wanting that to be explicit, like trait implementations in Rust. However that would only work to provide an implementation to an interface. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
One of the restrictions that C#'s nominal type system has is that it forces people to wrap and convert matching concrete types and frequently hand roll adapters between types where they structurally match the requirements of other use cases.
Acknowledged Prior Discussions
I'm aware that there have been a number of swings at structural typing in C# over the years but I'm unaware of a discussion that matches this suggestion.
Prior art includes:
This is very similar to the goals of the Roles proposal, but a different approach to the problem
Ref: Roles and extensions
This also has similarities to extension types, where users could define an extension style mapping between types in their codebase
Ref:
Other discussions
The purpose of this discussion is to discuss if a keyword-based approach might be lower ceremony and simpler implementation.
Problem
This process is often error prone or relies heavily on third party libraries - AutoMapper for DTOs, Mocking libraries, or the proliferation of mapping overloads just to coerce data-types.
I speculate we could enhance the language with a new or enhanced keyword to remove entire categories of package use and "glue code" without sacrificing safety.
Proposal - A "trust me bro" structural typing keyword + checks
In the following examples I'm going to use the invented keyword
structurallyIs
.I think we could perhaps use the existing
is
/as
keywords to the same end, but for clarity here I'm using an invented keyword to highlight what could work without potentially confusing the examples.The key outcomes are:
When two types structurally match, using a keyword you can tell the compiler to verify that they match
When a strict subset of the class members are used, a narrower implementation can be provided and the compiler should verify only the implemented members are used by the calling code
I speculate this could be achieved by doing code gen of adapter classes (maybe "AutoAdapters" as a language concept), or some duck-type compiler trickery (maybe the receving type grows an
object
taking overload at compile time or something).Some examples:
Thoughts
Most of these scenarios apply when the developer doesn't have the ability to modify either the target or destination types.
This is common when:
I see these scenarios often in large scale enterprise apps and think this could be a great unlock for the language.
I think this is only a "good proposal" if the compiler is able, at compile time, to still verify the useage of these kinda duck-like structural type matches. The community is often fearful of anything that even looks like dynamic typing, and this should explicitly be called out as "not that thing".
Existing behaviour effectively is unchanged and the surface area of the suggestion feels quite contained.
Prior Art
I believe Go can do this today, transparently and without keywords, but that feels a little too radical for the language norms of C#
Beta Was this translation helpful? Give feedback.
All reactions