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
Feature request: Simplify generic notation #7763
Comments
I've sometimes avoided this kind of generic knots through helper interfaces. I know that your code sample is only supposed to be illustrative and simplified but I have to ask about why |
Yes, in my case it does care. My example doesn't perfectly describe my scenario: IHandlerResolver would be a better name than IHandler. Imagine a component (IHandlerResolver) which takes an IWrappedMessage and uses parameters on the wrapped message to do stuff like authentication/authorization/logging. It then locates an appropriate IHandler (IOC container?) and calls this handler with the wrapped message. I could take a non-generic version of the interface in and get the inner type through reflection, but that seems like a step back rather than forwards... |
I thought about same today. If C# would be able to infer generic parameters and their constraints on their declaration., it should cover original problem and lot's of similar problems.
Here created interfaces IA, IB, IC that reference each other and require their implementation by same concrete class group. It allow implement common logic for all implementation of this interfaces that would not obscure concrete class type, so that we still can do Main problem of such solution currently is requirements to write long generic arguments declarations and constraint even when they could be inferred. Let's suppose next rules: if class/function declaration starts with
But it can be improve even more, if we allow same mechanics in generic argument usage.
Feels that last one is very raw, but we can discuss it. It also should relate to calling method with inferring only part of generic arguments - not sure if there is already issue with such discussion. |
Sounds a bit like #7850 😄 |
Related issue: #5023 |
@ChristianRygg why not public interface IWrappedMessage<out TMessage> where TMessage : IMessage
{
TMessage Message { get; set; }
}
public interface IHandler<in TWrappedMessage>
where TWrappedMessage : IWrappedMessage<IMessage>
{
bool HandleMessage(TWrappedMessage msg);
} Though, the whole design can be reduced with higher kinded generics (#2212). |
@alrz I realize that my example doesn't really express the problem clearly enought, but I really would prefer getting the actuall class rather than the interface when handling the message. I could of course do my own casting and type checking, but in my mind this is even more complex than the situation I was hoping to simplify... |
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. |
I love using generics, but tend to end up in the same mess everytime I get carried away with them: At some point interfaces get a long list of generic types, most of which theoretically can be inferred from the rest.
Take this simplified example (with real examples having longer lists of types):
In this case,
EmailTextMessageSender
is a handler forEmailTextWrapper
and theTextMessage
type is given fromEmailTextWrapper
.Would it be possible to simplify the notation to not have to specify the second type somehow? I see that it might be needed if the type should be used directly in the interface, but perhaps it is possible to only specify it at the highest level (in this case the
IHandler
interface) and not in the implementation, something like this:Perhaps it's just my coding style, but this would be a great feature so both simplify coding and making the resulting code more readable.
The text was updated successfully, but these errors were encountered: