Replies: 20 comments 1 reply
-
Marking a complete method as 'checked' and then unchecking in it seems confusing to me! It counters the whole purpose of this proposal. However having it on the method declaration's modifiers makes sense for those who use it. To me, not so much on a class' declaration, unless every method is decorated with |
Beta Was this translation helpful? Give feedback.
-
|
I like this just for symmetry with |
Beta Was this translation helpful? Give feedback.
-
This is already allowed with using System;
public class C {
public void M() {
checked
{
// Compiles fine!
unchecked { int i = 3; int j = i + 1; }
}
}
}
I get your point. However, I think |
Beta Was this translation helpful? Give feedback.
-
|
How does this interact with inheritance and interfaces? Can you declare a method to be (How) does it interact with overload resolution? |
Beta Was this translation helpful? Give feedback.
-
|
Shouldn't be any different than the |
Beta Was this translation helpful? Give feedback.
-
If a base class is marked
It will not affect method signatures. Edit: I just updated my proposal in response to all of the questions you raised. |
Beta Was this translation helpful? Give feedback.
-
|
It seems strange to me that you desire new syntax to use just about everywhere in your project, instead of just defining the project property for making everything checked. That said, I could see this being useful on a regular basis for |
Beta Was this translation helpful? Give feedback.
-
|
Let's leave |
Beta Was this translation helpful? Give feedback.
-
|
I don't think that's generally a good enough reason to leave them alone. Every GetHashCode implementation I ever write would look immediately better with an unchecked method modifier. |
Beta Was this translation helpful? Give feedback.
-
|
@jnm2 I'm actually fine with method implementations accepting the modifier, but not |
Beta Was this translation helpful? Give feedback.
-
This sounds fair. It would be just like |
Beta Was this translation helpful? Give feedback.
-
|
@whois Definitely. It's an implementation detail invisible to the type system no different than |
Beta Was this translation helpful? Give feedback.
-
|
@whoisj I agree. It wouldn't even make sense to have them as part of |
Beta Was this translation helpful? Give feedback.
-
|
Given the proposal #686, I'll throw out an alternate syntax for this scenario. public static int Choose(this int n, int k) checked
{
...
}or perhaps some would prefer this formatting: public static int Choose(this int n, int k)
checked
{
...
} |
Beta Was this translation helpful? Give feedback.
-
|
For that matter, is there a way to use public static int Choose(this int n, int k) =>
checked
{
...
}; |
Beta Was this translation helpful? Give feedback.
-
I think the current proposal is most consistent with how
public static int Add(int a, int b) => checked(a + b);The way you have written it will not work because |
Beta Was this translation helpful? Give feedback.
-
|
Thanks, I was not aware of the expression form. |
Beta Was this translation helpful? Give feedback.
-
|
@gafter Does this proposal have enough popularity/sound reasonable enough to warrant discussion in an LDM? |
Beta Was this translation helpful? Give feedback.
-
|
One problem I ran into as well is that as far as I can tell, there is no way to use a checked context for "statement-bodied" members like this: public void Increment() => _position++; None of these work: public void Increment() => checked(_position++); This proposal would fix that. |
Beta Was this translation helpful? Give feedback.
-
|
Hi, I have made an alternative proposal which might be another way to avoid excessive blocks and indentation: #9707 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Background
checkedanduncheckedare C# keywords that control behavior in the event of an integer overflow. I frequently use checked arithmetic in code I write, because it eases debugging and reduces the chance of errors slipping by silently. I also prefer to be explicit by puttingcheckedin the code, rather than relying on a project file to tell the compiler to use checked/unchecked arithmetic by default.As a result, I have many methods in my codebase that do a little arithmetic that look like
Where I wrap every arithmetic operation with
checked(). I could also put the whole method inside achecked { }block to avoid forgettingchecked()lest I introduce another operation, but I'm hesitant to do so because blocks add another level of indentation:Proposal:
checked/uncheckedas a method modifierI propose making
checked/uncheckeda valid method modifier. The following forms of code will be allowed if this is implemented:This would be semantically equivalent to writing the following today:
Here is how my above example would look like using this new feature.
Precedent:
unsafeIf approved, this feature would not be entirely unprecedented.
unsafe, which allows developers to demarcate regions of 'unsafe' code where they can use pointers, is already valid as a block keyword and as a method modifier. You can write both of the following today:It doesn't seem unreasonable to make the story the same for
checked.checkedas a type modifierunsafebrings me to my next point: shouldcheckedbe allowed as a type modifier? While writing this proposal, I initially hesitated to allow it to be a type modifier because I didn't see the need for it. However, I have changed my mind: first of all,unsafe class/unsafe structis allowed (socheckedshould follow precedent), and second, I do have many types where I usecheckedin multiple different methods. Instead of annotating each method withchecked, it would be convenient to write it once and have it apply to the whole class.If we allowed
checked/uncheckedto modify types, the following code would become valid:Nested
checked/uncheckedIntuitively,
checked/uncheckedon the inside should have stronger precedence than on the outside, so that is what is proposed here. Expressions > blocks > methods > types for determining what type of arithmetic is used.Local functions
Like
unsafe,checked/uncheckedwill be permitted on local functions. Local functions will have greater precedence than their containing method on usingchecked/unchecked.Edit 5/9: These modifiers will not affect the type system or change method signatures; they are purely an implementation detail of types/methods marked with them. A derived class is not automatically checked if its base class is marked
checked, for example. Avirtual checkedfunction will not make overrides automatically checked, either. Also, a couple of places where it doesn't make sense to use these modifiers will be banned from doing so:abstractmethodsexternmethodsEdit 5/11: This occurred to me while I was working on my project: allowing
checked/uncheckedmethods would also help reduce code density in expression-bodied members. Consider the following snippet of code I was just editing:I would like to eliminate the
checked()because IMO, there's already enough going on in the method. However, to do that I would have to put the entire ternary expression in achecked { }block, forcing me to not use an expression-bodied member here. With the above proposal, that would not be necessary:Beta Was this translation helpful? Give feedback.
All reactions