-
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
Proposal: Add support for appending ! to method names to denote method mutates object passed in. #2696
Comments
!like |
Practically, here and today, you can just use the JetBrains's one, which will give you a proper IDE support: http://www.jetbrains.com/resharper/webhelp80/Reference__Code_Annotation_Attributes.html#PureAttribute Even Entity Framework 7 team is using those: https://github.com/aspnet/EntityFramework/search?q=jetbrains Besides that:
|
How would delegates work? Wold they require that
Is it a boolean expression or a statement? Can I assign a non What about overloads? Is Is this |
@paulomorgado it doesn't need to necessarily be a ! it could be anything, just some indication that a method is dangerous. I would think that they would infact be overloaded in the scenario you mentioned. I would think it would be cosmetic/conventional but could be worked into things such as CodeContracts and tools like ReSharper could provide hints to the user as well. |
Considering that "not-safe" or "not-pure" is the default today I think changing the syntax required to specify that default is a bit of a non-starter. It would require that the majority of code be modified in order to support this syntax, even if it were opt-in. How does this not have the same problem that decorating a method with I think that a good analyzer that can verify methods marked as |
@HaloFour I'm proposing that it explicitly not be implemented as an attribute as that has issues (i mention them in the original post). Calling an Expand!() method tells every caller that the method can cause side effects to object your passing in. This does not need to be enforced by the compiler, it can just be a convention for users. As a user it is extremely useful for me to know that the method i'm calling will modify my object, instead of needing to experiment to find out how the method will act. |
So really this proposal is to permit the use of the |
@HaloFour Ruby already has this convention, i think it makes more sense to adopt it directly from Ruby as a lot of people can infer what it is implied by the !. And eventually perhaps compiler behaviour can be added to support it, not only as a convention but maybe someday as a feature. |
I'd rather a C-family convention, which would be marking "pure" methods as "pure" and not vice-versa. Attributes are already provided for this effect. Ruby is a very different language syntactically and behaviorally. I doubt that there is all that much cross-over and I doubt that most C# programmers would understand this |
@HaloFour An attribute doesn't tell me anything about the code i'm about to call unless i look at the source code, which may involve decompiling and inspecting the calling method it's compiled into a dll.
|
|
Anywho I think we'll just have to agree to disagree. I do agree that there is value in being able to easily identify methods that are pure/mutating as well as have the compiler/analyzers provide better diagnostics around them. |
@HaloFour I appreciate you bringing up those points, it's always good to get a look at the other side of things. |
@OmarElabd, you can always use |
I now think you are both right, in a way that the new syntax is not needed but the attributes are not visible to consuming developers in any useful manner. I have already asked the Roslyn team to address this unfortunate problem, but they are reluctant. As if they see attributes as some necessary evil that must be swept under the carpet #711.
Could you then please list all those wonderful IDEs? Unless I am doing something wrong the only way to see attributes is by going to sources. The only random exception is made for the ObsoleteAttribute for an arbitrary reason #781, and even then you have to hover over for a tooltip. Last time I checked the only way @OmarElabd can get something close to what he wants is by using Visual Studio in conjunction with ReSharper and the properly configured Enhanced Tool-tip plugin: https://github.com/MrJul/ReSharper.EnhancedTooltip |
You're right that VS doesn't provide any visual indication of pure methods or for arbitrary attributes today. You'd need an extension which should be fairly trivial to write. If/when the Roslyn team does decide to pick up proper support for "pure" methods I'd hope that they would consider ways to identify them better in the IDE. |
@HaloFour |
Sure, but they each have to do their own thing as it is to support basic syntax highlighting and code completion. Indeed it's not as obvious as something like a naming convention. Is a naming convention applied this late in the game really any better, though? Especially one where the undecorated methods are assumed to be "safe/pure"? The vast, vast majority of methods called aren't going to be decorated with this naming convention, or be safe/pure, which would be confusing to any developer on that project. If I was going that route in my projects I would likely adopt a convention to mark the pure methods instead. |
@HaloFour Why not both? '!' for unsafe '*' for safe? That way the lack of an operator implies nothing, but the presence of one immediately tells you something. |
@OmarElabd Why not |
@HaloFour the biggest issue is we do not want to mistake previously written libraries and code as pure/unpure. An existing method called RetrievePure() would be misconstrued as a pure method when it is not because Pure in this case (that is in the RetrievePure() method) is actually an acronym or something else. Whereas because special characters have never been allowed previously in method names this issue does not exist with special characters. |
@OmarElabd But |
It's going to get confusing since there are already proposal in which '!` denotes non-null |
I find myself agreeing with @HaloFour. C# has a long history of assuming mutability and impurity. Asking developers to change their mental model to meet parity with Ruby isn't going to go over well. I would expect a mass rejection. However, C++ already has a model that works well that C# should follow. Why not invert this discussion and mark methods that do not modify their object using the
The concept could be expanded to parameters as well (like the original posted is asking for):
|
@whoisj Actually that sounds like a really good idea, const could be used for methods that do not modify methods and something like 'volatile' could be used to indicate the opposite. There would need to be some sort of indication from intellisense that a method is marked as 'const'. |
This is one of the fundamental features of the Code Contracts Editor Extensions extension, which is under active development. |
That is not what the First off, although it has already been stated, I want to repeat that, in Ruby, there are no semantics associated with It is also not about side-effects. There are plenty of side-effecting methods that don't have a There are also plenty of mutating methods without OTOH, there are methods like What the So, In Scheme, In Reia,
into
Note that this is different from mutating |
We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages. |
This comes straight out from ruby conventions for denoting methods with side affects. That is that the method will modify the object is it called on.
Consider the two following methods which double the size of a square, the first one is "dangerous" as it mutates the object that is passed in.
The second method does not make any changes to the original object and is "safer".
The closest current equivalent is the [Pure] attribute in the code contracts (although it denotes the opposite). However there are numerous disadvantages with using that attribute:
The text was updated successfully, but these errors were encountered: