-
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: Declare new primitive types with constraints #1795
Comments
So like Contracts? #105 |
Yes something like contracts, but the proposal #105 is for contracts on functions. This proposal is for constraints, guards, contracts, or whatever you call them on types, and thus enforced every time one uses the type. I think this proposal and the contracts proposal could enhance each other. |
@thargol1 You're proposing few things here not just contracts.
The moment people will ask how this is going be lowered by the compiler is the moment you will understand it's exactly just like #105 but maybe more succinct because it's more declarative, meaning, the compiler will have to generate a bunch of stuff for this to work. So my guess is that you need both something like #1711 and #105 as the building block for this proposal. |
It actually does, it provides preconditions, postconditions and invariants. And in CodeContracts it was something like this:
Not sure whether it's directed to my comment but I didn't say it's a duplicate, I meant that lowering the declarative code will end up with something you would have written yourself if you had #105 in place and the reason #105 is essential for similar proposals for them to even consider it. |
@bondsbw Pretty simple, here is an example:
The static checker would have caught your invalid assignment at compile-time because the contract was violated. p.s. These contracts exists in the full framework today but because they are meant for CodeContract they do nothing by themselves so don't expect them to throw. Just for reference here is the same version of the code without
|
I'm aware of contracts, but the amount of code you need is not to my liking. Many recent C# changes over the year reduce the amount of code needed (arrow-functions, auto properties, tuples, var and the C#8 Record?. So if the compiler translates something like this type age = int where value >= 0 && value <= 150; into a struct with contracts, implicit operators, equals operators etc etc it would make life a little bit easier. public struct Age(int value) where value >= 0 && value <= 150; To be more in sync with the proposed Record classes. |
We currently don't have records in the language and as far as I can tell I can't see it in C# 8.0 but in your case you need something like #1711 more than records. I probably didn't say it enough or phrased it poorly so I'll try once more, you need the primitives of #105 to allow any proposal similar to yours to work. Once you have the two proposals above making it into the language, proposal like yours would have legs and might be considered but unfortunately I can't see #105 happening anytime soon or far future. |
Allow the creation of compile time only types that are primitive types with constraints attached. This allows for better code with less typing and less errors.
Examples
Declare a new type with constraints:
Compile time error:
Compiler generated argument check code:
Compiles to:
Conversions:
Compiles to:
The text was updated successfully, but these errors were encountered: