Idea: Strong Type Wrappers #9223
Unanswered
jpergler
asked this question in
Language Ideas
Replies: 2 comments 5 replies
-
Roles? #8431 |
Beta Was this translation helpful? Give feedback.
3 replies
-
I would be interested to see someone attempt to address this problem with a source generator and see how far they get. Basically, I would be reluctant to invest here on the language side, except if there is some deeply compelling scenario here which can't be addressed by a source generator. [StrongTypeWrapper(typeof(string))]
public partial struct Email; |
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.
-
Summary
This idea proposal introduces a new
strongtype
keyword to C# for creating strongly-typed wrappers around primitive types with minimal boilerplate. The feature is primarily driven by two motivations:Motivation
In many C# applications, developers frequently need to create lightweight wrappers around primitive types for type safety, domain modeling, and input validation. For example:
Email
type that validates its formatOrderId
type to prevent mixing different ID typesPercentage
type that ensures values are between 0-100Currently, implementing such wrappers requires repetitive boilerplate:
Moreover, integrating these custom types with frameworks like Entity Framework, ASP.NET, and other tools that use reflection or serialization requires additional configuration:
A native language feature would enable transparent integration with these frameworks, allowing entities to use strongly-typed properties while the underlying storage and serialization would work with the primitive types seamlessly.
Syntax
Basic Definition
With Validation
This validation check goes into account whenever the
strongtype
is instantiated or deserialized so it is ensured the instance always meets thewhere
condition or the exception is thrown.With Additional Members
These members are only getters. They server to extract some structured information from the default internal Value.
With Validation and Members
Complex Validation Block
Inheritance
Secondary Constructors
Entity Framework Use Example
Integration with Validation Frameworks
Potential Implementation Approach
This feature could be implemented as a compile-time transformation similar to how Kotlin implements inline value classes:
Benefits
UserId
vsGroupId
)Key Design Considerations
Validation Behavior
The strong type's validation occurs at instantiation time, ensuring that no invalid instances can exist:
Validation of the
strongtype
object is not meant to replace general data validation especially for user inputs. When user input needs to be validated usually a string value is needed and some validation mechanism such as attribute annotation validation takes place.The validated string values then can be put into
strongtype
object which ensures that a static format is met for example on API calls when the value is deserialized or when it comes from database.Type Erasure
At runtime, strong types would be replaced with their underlying primitive type:
Type Safety and Equality
Different strong types are not equal, even with the same underlying value:
Comparison with Other Languages
This proposal is similar to Kotlin's inline value classes, which solve a similar problem:
However, this proposal extends the concept with:
The
strongtype X of Y
syntax is more explicit about the wrapping relationship than Kotlin's approach, making the code more self-documenting.Open Questions
strongtype
, alternative keywords likewraptype
orwrapper
could also be considered. And there are more syntax challenges as well.Conclusion
I see some patterns which repeat again and again when implementing strongly typed wrappers above primitive types. This is an attempt to bring an idea of syntax which can be self-explanatory and aims for specific problems in code which many developers solve on a daily basis.
The
strongtype
feature would provide a powerful tool for domain modeling and type safety in C# applications. By eliminating boilerplate while maintaining transparent framework integration, it would significantly improve code quality and developer productivity.I understand that there are many unanswered questions. I will be happy for any feedback or just to bring a little light on this topic or different thoughts.
Beta Was this translation helpful? Give feedback.
All reactions