[Proposal]: Compatibility through coexistence between extension types and extension methods #8918
Replies: 7 comments
-
This is an important difference between the two proposals. The proposal I just opened strives for perfect semantic guarantees while allowing all extensions to move forward. We should discuss as a group if that's important. Personally, I think it is. Having a mishmash of old and new isn't going to feel good, and I'm unsure if we'll even convince users they can do so safely. This risk both the community and internal code bifurcation I call out in my speclet. I would like people to feel that the can completely safely move to the new form without any risk, and then enjoy both the migrated methods and new members living together harmoniously. Of note: much of the concerns raised here (attributes, refness, merging, etc.) simply fall out. So I personally think the other approach strikes a nicer "no compromises" approach. :-) |
Beta Was this translation helpful? Give feedback.
-
Maybe just forget about the new syntax completely and have it be implemented in the old-style, with static methods in static classes, but making the compiler recognize such methods as properties and operators, with something like this:
It turns out the above syntax for user-defined operators is already valid and recognized by the compiler, though not at design-time, only when it comes from metadata. For the property case, you could have the compiler recognize the And of course, adjust the process of symbol lookup to find these new fake properties and operators. |
Beta Was this translation helpful? Give feedback.
-
This is slightly surprising. I assume it will still be true, even if it is not a goal? I take this also means the "old syntax" will never really be deprecated, but perhaps the new approach will be encouraged, say, using an analyzer+code fixer? |
Beta Was this translation helpful? Give feedback.
-
All current extensions could be ported over to work the same (or very close) as extension methods. But they won't also show up as static methods that you can call directly.
Correct. The goal is that there's no reason to use the old syntax for new code going forward. |
Beta Was this translation helpful? Give feedback.
-
I currently expect to sometimes prefer the old syntax going forward no matter what new syntax becomes available. It's hard to beat adding/removing just the token |
Beta Was this translation helpful? Give feedback.
-
This could be if the syntax could be relaxed a bit. Since right now all extensions are non-generic (and static), to cover generic extensions, partial extension<T> Enumerable for IEnumerable<T> { // static class Enumerable;
public IOrderedEnumerable<T> OrderBy(...);
}
partial extension<T> Enumerable for IOrderedEnumerable<T> { // the target doesn't need to match
public IOrderedEnumerable<T> ThenBy(...);
} This will emit the exact same Enumerable class that we have today. With that I think it makes more sense to make the name optional for local usage where the name is not going to matter, file extension for string {
// ..
} For internal extensions that could suffice 99% of the time where you don't need to disambiguate. |
Beta Was this translation helpful? Give feedback.
-
Proposed additions 1 and 2 completely cement the "static class" approach for new extensions. Does that mean interface implementation is out the window? If not, what happens in this case? extension FooExt for Foo : IBar
{
public void Method() { }
public void Func() // IBar method
{
this.Method();
}
public void OtherMethod()
{
this.Func();
}
public Action GetDelegate() => this.Func;
} How is the above emitted? What about delegates' Addition 3 makes sense in any case. What about languages which don't (at least for some time) recognize C# extensions? If we make them static classes, then using them in other languages necessitates doing a static class invocation, which is fine, but it means changing to a struct wrapper is impossible. I still think that way too much effort is being spent to ship extensions without the underlying runtime changes needed to deliver the full feature with interfaces. In the process it's getting more and more confusing. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Compatibility through coexistence between extension types and extension methods
This has been moved to https://github.com/dotnet/csharplang/blob/main/meetings/working-groups/extensions/Compatibility%20through%20coexistence%20between%20extension%20types%20and%20extension%20methods.md.
Beta Was this translation helpful? Give feedback.
All reactions