-
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
Discussion: Constrains for numerical types? #16257
Comments
I think that this falls firmly under Method Contracts. I certainly wouldn't want one way to enforce numeric "constraints" vs. enforcing method contracts. I'd suspect that 99% of method contracts would be validating non-null references and ensuring that numbers are within range. |
I don't think that we will ever see method contracts and this is the reason I'm thinking about ways to somehow have a slim down version to at least have preconditions formalized as part of the language as constraints.
This is the reason I'm thinking about it. Non-null references can be or should be implied whereas range needs to be explicit so I thought that the way we express constraints for generic parameters can be a good fit. |
I am very interested in constraints, but I would rather wait for general solution with method contracts. |
Perhaps, but the reasons that method contracts might not happen would apply equally to this proposal. Specifically how the constraints would be encoded in the assembly metadata and exactly how the constraint is enforced when violated. Your proposal doesn't touch on either. |
@HaloFour This isn't a proposal, just a discussion... 😄 |
I'd love it myself but seeing that there were many attempts and the design team still think it's a bad idea or that it's too complex or too verbose I'd say the odds aren't in our favour. |
Either way, I'd argue that it's the most important aspect of the discussion. Source generators could probably handle the simpler cases. Toss attributes on the parameters and the generator could emit replacements which perform argument validation. |
The issue is that two different implementations may generate different attributes for different projects and you may end up needing multiple source generators, another issue is you don't really want to fail-fast at run-time, you want compile-time errors and analysis. Now, we can probably implement this through analyzers but there's no formalized attributes that are standardized for this so each 3rd-party may have its own thing. |
I'll think about it, I mostly think about having the information embedded as attributes but it's a bit cumbersome. There are some high level questions like:
I think that the first part should be about having the information available so analyzers can take advantage of this or something like this. p.s. I think you want this information or at least prefer this information to be part of the PE metadata but I'm not sure what changes this require. |
Maybe it is better not to work with construcors (special case) in constraint examples. Let we take general method with slightly changed syntax (#119).
@eyalsk I don't think that attributes are good approach to encoding constraints in assembly metadata. It is too complicate and impractical. Compiler could easily generate private hidden method for every constraint and every constraint type (requires, ensures).
I put throw Exception in the case of violation, but it is open question. |
Yeah that is the reason I've closed this discussion, I've played with it and it seems really chaotic. 😄
The best second place I can think about adding this information is xml comments but yeah the best place is in the PE metadata, the question what needs to change in order for this to take place and how complex this change is and then I'm not sure whether it impact compatibility. I think that the approach you're proposing can probably be done with generators but it has the same problem as I pointed above |
At the moment we can have constraints for generics parameters, I wonder what people think about constrains for the native numerical types?
I know it's related to Design by Contract but I think that this is a more simple problem and isn't so ambitious and it can always be expanded in the future either to support more types or/and user-defined types.
So what I'm thinking is something like this:
Bad idea? good idea? what do you think?
The text was updated successfully, but these errors were encountered: