-
Notifications
You must be signed in to change notification settings - Fork 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: Shorter syntax for simple structures that encapsulate a single value. #3170
Comments
Source generators could also be another solution to this problem. |
This is just a record with a single member? |
Yes this looks like a record with a single member, but a record is a class and not a struct (#2699). In order to migrate existing code, converting existing integers and strings is easier with structs, just change the type and you're done. With classes you have to add new-statements etc. And classes are just slower in performance (not a lot). Because this is a single member struct, a lot of boilerplate code can be generated, like automatic type conversions from and to the underlying type, which in my opinion is a must to facilitate migrating existing code. I'm still reading #1695, but it looks like I can close this issue. |
This feels like a space for an IDE feature. Feels like we could easily add a 'create wrapper struct' feature where we generate all that conversion logic and we allow you to specify what you want to expose from the wrapper. |
@CyrusNajmabadi if everybody implements their own version of this, then there can't be any third party support for eg: serialisation for almost-a-string Email(string e) (as mentioned in the 'cons' of the FP) This is enough of an issue that visual studio always shows variable names on the calee of a function just to provide you the visual hint to not put the email into the username, but this would provide a Guarantee with a few extra lines of code. The only question IMO is "is this extra complexity in the language worth the bugs it will save, via bringing extra context based type-safety to use of int,string, and bool" |
I'm closing this issue and will add suggestions to #3137. It looks very similar but it's all in the details. |
Inspired by some epic bugs in our codebase, I created 'simple datatypes' for stuff like 'Email', 'Emplid', 'Usercode' etc.
It prevents bugs like:
old code:
My datatypes look like tthe code below... a lot of standard boiler plate code. I don't want to use classes because of the extra memory they use and they require more changes to existing code.
This is a complete example of an email datatype:
The problems with this code:
Converter code for NewtonSoft
However if this became a feature in the C# language the creators of NewtonSoft, Dapper, EF core could build standard support for it.
Syntax proposal:
This generates the code above without the validation and TryParse
This genates the code above including validation and TryParse.
An example I wish I could use right now:
Pro's of using this kind of simple datatypes:
Cons:
The text was updated successfully, but these errors were encountered: