Relax CS0571 (cannot explicitly call operator or accessor) #6510
Replies: 5 comments 10 replies
-
These helper methods are just implementation details - it would be possible to implement them in different ways. I would not like to leak those details. You can wrap every property accessor in a lambda, so you get a proper delegate: var c = new MyClass();
var c_setValue = (MyClass c, int v) => c.Value = v;
var c_setValueCaptured = (int v) => c.Value = v;
c_setValue(c, 42);
c_setValueCaptured(44); The compiler could recognise such lambda creations and optimize them away... |
Beta Was this translation helpful? Give feedback.
-
Is will be a major breaking change for 20 years of code. Nobody will change it.
It's a well-known detail of how properties are implemented. By the way, F# allows to do same thing open System
type MyClass() =
member val Value : int = 0 with get, set
member _.Item
with get idx = 0
and set idx value = ()
[<CLIEvent>]
member _.Changed = Event<EventHandler, EventArgs>().Publish
let c = MyClass()
c.set_Value(42);
c.get_Value();
c.get_Item(7);
c.set_Item(7, 42);
c.add_Changed(fun sender e -> ());
c.remove_Changed(fun sender e -> ()); |
Beta Was this translation helpful? Give feedback.
-
Generally speaking, this is an appropriate argument when backed with feedback from said group. i.e. if we actually saw tons of people asking for this, then that would help make the case that people want this and we should reconsider. However, if it's been 20 years and there's only a single ask for this in all that time, it seems far more likely that very few people actually want this and the existing solutions are sufficient in practice. |
Beta Was this translation helpful? Give feedback.
-
I would like to call operators explicitly, because I'm trying to figure out why
As such, I think the only option is for the language to provide this feature. I see no reason why syntax for an alternative way of invoking the code that implements an operator shouldn't be provided. |
Beta Was this translation helpful? Give feedback.
-
I would like to suggest two different approaches: 1 - Instead of exposing the accessor methods directly, maybe C# could introduce the 2 - If the problem is just the extra allocation, then I suppose Roslyn could improve the management of captured variables, such that if a single object reference is captured in the lambda, use that object directly as the delegate target, instead of synthesizing a "DisplayClass" to contain the variable and method body. With this approach, the body of the delegate would become a static method, that takes the captured object as the first parameter, and the delegate would be a "closed delegate pointing to static method", with the captured object as the delegate target. This kind of delegate can already be produced currently through the use of extension methods, you can use an extension method as method group, and the delegate will point to that method directly, while capturing the "this" parameter as the target. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
CS0571 is generated whenever someone tries to access underlying method of accessor or operator. For example
All of the invocations raise CS0571. This behavior seems redundant, compiler should warn or tooling should just hide members (like when
[EditorBrowsable(EditorBrowsableState.Never)]
is used). I guess it was introduced at C# 1 timeframe, when properties were new feature for mainstream language and compiler team decided to force users to use proper syntax. It was fine and justified for some period of time, but now a lot of methods take delegates as input and some people may want to get better performance by reducing allocations. One problem is that codefoo(() => capturedVar.Property)
produces 1 more allocation compared tofoo(capturedVar.get_Property)
.Today I've had a need to pass delegate, which would take item from dictionary
Another example is System.Reactive:
I ask to relax CS0571 and either
Beta Was this translation helpful? Give feedback.
All reactions