C# Inherited Open Generics Compile -
today brain went dead, , couldn't figure out clean way of forcing compiler use inheritance generic inference.
imagine following 4 classes
models
public abstract class model { } public class codeperfmodel : model { } entities
public abstract class modelentity<tmodel> tmodel : model { public tmodel model { get; set; } } public class codeperfentity : modelentity<codeperfmodel> { } now me logically should take granted when take inherits modelentity<> (it specify type of tmodel) via inheritance, because class inherits modelentity<> have specify it.
is there anyway force compiler figure out me?
e.g.
if want use modelentity<>, have specify type it. such following:
public class callerclass<tentity, tmodel> tentity : modelentity<tmodel> tmodel : model { } how can rid of tmodel argument everywhere? while still having access tmodel type @ compile time? e.g. via base model property.
to me, following:
public class callerclass<tentity> tentity : modelentity<> { } would make perfect sense when calling should have speicfy e.g.
somecall<codeperfentity>(); rather than
somecall<codeperfentity, codeperfmodel>(); is possible?
would worth raising c# 6/7?
you mention access tmodel @ compilation time, while not explicitly specifying type when deriving class. letting go of example, , moving more general case, means semantics remain same, not explicitly declare type parameter's own type parameters when declaring generic constraint.
in essence, asking why specific syntax sugar feature not implemented.
let's consider example:
public class callerx<a, b> : modelentity<> b : modelentity<> from example in question, compiler should insert tmodel'1 , tmodel'2 type parameters a , b respectively. let's feature implemented. means have created default situation tmodel'1 , tmodel'2 different types, each having constraints match single type. if add more constraints either tmodel'1 or tmodel'2, or force them same type? why case special deserves own syntax?
from know of c# team, have policy each new feature starts "-100 points" , should great in order considered (see uservoice c#).
to summarize:
- new language features expensive , add complexity.
- you asking implicit syntax unlikely/unclear desired situation in of cases.
- developers have learn , understand open generic type type parameter constraint insert hidden , anonymous parameter. me, not intuitive other type parameter has been added type without me having declared it.
Comments
Post a Comment