[Proposal]: allow the keyword interface as generic constraint #3964
-
allow the keyword interface as generic constraint
SummaryThis feature would allow restricting generic parameters for any interface. MotivationThe motivation is to make generic constraints more flexible and restrictive. With the given proposal the caller could specify any interface instead of a specific one. Detailed designthe following code does not compile: public void RegisterService<TService, TInterface>()
where TService : class
where TInterface : interface // <- CS1031 "type expected"
{
_containerBuilder.RegisterType<TService>().As<TInterface>();
} DrawbacksAlternatives1.) A possible workaround would be to create a "markup interface" which is empty and use it for the constraint. 2.) Not to restrict for only interfaces and use Unresolved questionsDesign meetings |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 5 replies
-
That's not really a motivation. Why do you think such constraint would be useful? |
Beta Was this translation helpful? Give feedback.
-
With the given proposal the caller could specify any interface instead of a specific one. Will update the proposal. |
Beta Was this translation helpful? Give feedback.
-
@BrayanKhosravian That still only describes what the proposal is, but not why should it be added to the language. |
Beta Was this translation helpful? Give feedback.
-
I'm going to move this to be a discussion. I'm also not convinced why this needs to be a constraint, and I think some discussion around the topic would be better before making this a full proposal. What issue are you trying to solve? What code does this enable that wasn't possible before? Motivating examples are very helpful for trying to understand why you want to see this feature in C#. |
Beta Was this translation helpful? Give feedback.
-
What exactly is the point of restricting a generic argument to an interface specifically? You can't use that as a common abstraction, as interfaces by themselves have no API surface, and you cannot instantiate them, which means any object you can pass into them would fail such constraints. It's a proposal that defeats itself. |
Beta Was this translation helpful? Give feedback.
-
I'm not seeing the scenario here that makes this a feature that's needed? The The language team can't implement a feature if they don't know what that feature is supposed to do and have scenarios around what it will enable (or what it prevents). As for the referenced prior proposal, after having read through it, it didn't go well because there were no explanations of what the feature meant to achieve and the entire tone was combative (at least, that's how I interpreted it). |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Thanks for all the responses. Here is a concrete example about why I think this could be useful. Ofc this is not a must-have feature but it would be nice. interface IService
{
void Execute();
}
class Service : IService
{
public void Execute() => throw new NotImplementedException();
}
class IoC
{
private readonly ContainerBuilder _containerBuilder = new ContainerBuilder();
public void RegisterService<TService, TInterface>()
where TService : class
where TInterface : class
{
if(!typeof(TInterface).IsInterface) throw new ArgumentException($"{nameof(TInterface)} has to be an interface!");
_containerBuilder.RegisterType<TService>().As<TInterface>();
}
}
class Program
{
public static void Main()
{
var ioc = new IoC();
ioc.RegisterService<Service, IService>(); // <- this method should be used like this
ioc.RegisterService<Service, Service>(); // <- i dont get a warning or error here at design time but im throwing an
// exception at runtime
}
} |
Beta Was this translation helpful? Give feedback.
*wink wink, nudge nudge*