Replies: 16 comments 2 replies
-
There are some suggestions around the delegation pattern that were raised via Roslyn issues "back in the day", eg [Proposal] Deligation class and Proposal: C# interface delegation. Whilst I at least think providing something along these lines would be good, the language team didn't seem very receptive to it in the past. |
Beta Was this translation helpful? Give feedback.
-
The default interface implementation proposal can provide one way of implementing this request. |
Beta Was this translation helpful? Give feedback.
-
@gafter how? If I have interface |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox I have no idea if this is what @gafter meant, but I think you could do it by creating a second interface public interface IInterface
{
void MyFunc();
int MyOtherFunc();
// my other umpteen methods
}
internal interface IDelegatedInterface : IInterface
{
IInterface Instance { get; }
override void MyFunc() => Instance.MyFunc();
override int MyOtherFunc() => Instance.MyOtherFunc();
// …
}
public class MyClass : IDelegatedInterface
{
IInterface IDelegatedInterface.Instance { get; } // auto-property
public int MyOtherFunc() => 42; // not delegated
} One issue with this approach is that |
Beta Was this translation helpful? Give feedback.
-
@svick I see. I still have to write this delegation of umpteen methods. The big benefit is that I have to do it only once instead of once per implementing class. |
Beta Was this translation helpful? Give feedback.
-
I was making duplicate idea but difference syntax My propose is like this class SafeAddDictionary<K,V> : Dictionary<K,V>
{
readonly Dictionary<K,V> dict = new Dictionary<K,V>();
public void Add(K key,V value) // implement only Add
{
if(!dict.Contain(key))
dict.Add(key,value);
}
Dictionary<K,V>.this => dict; // syntax to be transpiled
// Transpiler implement everything except `Add`
} I think this should be able to do with just transpiling code at compile time |
Beta Was this translation helpful? Give feedback.
-
I was also going to make such a proposal, but then saw that #2017 had the syntax I had in mind. It seems there are a lot of people waking up in the morning and saying "Gosh, I sure wish I could auto-implement interfaces", and going ahead to make (or almost make) yet another proposal for the same feature. Perhaps that would imply that this is a useful and much desired feature? |
Beta Was this translation helpful? Give feedback.
-
Someone on the LDM would have to feel this was worthwhile enough to 'champion'. So far, no one has thought that is the case. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
One goal of a high level language is to avoid writing stuff that the computer can figure out itself. For example, c# offers auto-properties ( The impetus for this feature request (although I can only speak for myself; I haven't done a poll) is not to avoid typing, it's because I don't want that boilerplate code in my class. Also- What if there are a bunch of methods/properties on the interface? The answer to all of these questions is: a lot of drugereous and repetitive update work. |
Beta Was this translation helpful? Give feedback.
-
Yup, too bad. But that can change; people asking for/discussing features they want (without spamming - I tried to avoid that by looking at the open issues and commenting here instead of opening a new issue) are taking part in the design process by drawing attention to desirable features. If this is actually a good idea, and enough people are talking about it, maybe a 'champion' will take a look again. |
Beta Was this translation helpful? Give feedback.
-
Understood :) I was just letting you know the way this works here. Someone has to champion it.
Yup. That's how it often works. But there are literally hundreds of proposals with thousands of bits of feedback. And that's just here. There are many other avenues where feedback flows to the LDM. In general, they're routinely looking and reassessing things. If this ever makes the cut, they will let you know :) |
Beta Was this translation helpful? Give feedback.
-
This could be used to compose multiple interface parts:
We could even be wildly optimistic and hope that in the future this may even open the way to inheritance from generic type parameters:
|
Beta Was this translation helpful? Give feedback.
-
this has been my #1 pet peeve for years now.. |
Beta Was this translation helpful? Give feedback.
-
You can do this via Source Generators, using something like https://github.com/beakona/AutoInterface |
Beta Was this translation helpful? Give feedback.
-
There have been other syntax suggestions, such as I think the VS function or a source generator is not enough to address this, because "optimal" solution cannot be expressed in means of C# alone. For example: struct A : IFormattable
{
string IFormattable.ToString(string format, IFormatProvider formatProvider) => ...;
}
struct B : IFormattable
{
A val;
string IFormattable.ToString(string format, IFormatProvider formatProvider) => ((IFormattable)val).ToString(format, formatProvider);
} Visual Studio generates this implementation, but it is incorrect! Casting To implement this "properly", this code is needed in C#: string IFormattable.ToString(string format, IFormatProvider formatProvider) => ToStringFormattable(ref val, format, formatProvider);
static string ToStringFormattable<T>(ref T val, string format, IFormatProvider formatProvider) where T : IFormattable
{
return val.ToString(format, formatProvider);
} However the compiler could implement it in a more efficient way, without the generic method. In CIL: constrained. A
callvirt instance string [System.Runtime]System.IFormattable::ToString(string, class [System.Runtime]System.IFormatProvider) The compiler already generates similar code when calling |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We encounter the situation that we have to delegate the implementation of interfaces to an private instance of an object implementing the interface:
It would be nice to write a shorthand definition like this:
Beta Was this translation helpful? Give feedback.
All reactions