C # Inherited Open Generics Compile

My brain died today and I couldn't figure out how to get the compiler to use inheritance for general output.

Imagine the following 4 classes

Models

public abstract class Model
{

}

public class CodePerfModel : Model
{

}

      

Objects

public abstract class ModelEntity<TModel> where TModel : Model
{
    public TModel Model { get; set; }

}

public class CodePerfEntity : ModelEntity<CodePerfModel>
{

}

      

Now I should logically assume that when I take something that inherits from ModelEntity<>

(it will specify the type TModel

) through inheritance, because any class that inherits from ModelEntity<>

will have to specify it.

Is there a way to get the compiler to figure this out for me?

eg.

If I currently want to use ModelEntity<>

, I have to specify the type for it. For example:

public class CallerClass<TEntity, TModel>
    where TEntity : ModelEntity<TModel>
    where TModel : Model
{

}

      

How can I get rid of the argument TModel

everywhere? Having access to the type TModel at compile time? For example. through the base property Model

.

For me, something like the following:

public class CallerClass<TEntity>
    where TEntity : ModelEntity<>
{

}

      

It would be perfectly reasonable, like when calling everything I would have to use, for example

SomeCall<CodePerfEntity>();

      

but not

SomeCall<CodePerfEntity, CodePerfModel>();

      

Is this currently possible?

Would it be worth it for C # 6/7?

+3


source to share


1 answer


You mentioned that you want to access TModel

at compile time, but without explicitly specifying that type when getting the class. Dropping your example and moving on to the more general case, this means the semantics will remain the same, however you don't want to explicitly declare your own type parameters of the type parameter when you declare a generic constraint.

Basically you are asking why a particular syntactic sugar feature is not implemented.

Let's look at another example:

public class CallerX<A, B> where A : ModelEntity<> where B : ModelEntity<>

      



In the example of your question, the compiler should insert TModel'1

and TModel'2

as type parameters for A

and B

respectively. Let's say the function is implemented. This means that we have created a default situation where TModel'1

and TModel'2

are different types, each of which has constraints corresponding to one type. What if I would like to add more constraints for TModel'1

or TModel'2

, or force them to be the same type? Why is this case so special that it deserves its syntax?

From what I know about the C # team, they have a policy that every new feature starts at "-100 points" and has to be really big to be considered (see UserVoice for C #).

Summarizing:

  • New language features are expensive and complex.
  • You are asking for an implicit syntax for which it is unlikely / not clear that this would be a desirable situation in most cases.
  • Developers will need to learn and understand that a public type of a generic type inserts a hidden and anonymous optional parameter as a type parameter constraint. It is not interesting to me that some other type parameter was added to my type without declaring it.
0


source







All Articles