generics - Any way to have some type parameters inferred from other type parameters' constraints in C#? -
suppose have following code:
interface iwidget { } interface iwidgetfactory<twidget> twidget : iwidget { } twidgetfactory createfactory<twidgetfactory, twidget>() twidgetfactory : iwidgetfactory<twidget> twidget : iwidget { return ... }
whenever call createfactory()
must pass in both twidgetfactory , twidget type parameters. seems unnecessary, because twidgetfactory has constraint such specialisation of must specify twidget. there way can have twidget inferred automatically when calling createfactory()
, if must add kind of helper methods it?
(the above simple example, can more complicated in practice, save lot of complexity.)
without seeing body of createfactory
, difficult tell you're doing. based on return type, suspect createfactory
doesn't use twidget
type parameter interface. in case, relax generic constraints bit?
interface iwidget { } interface iwidgetfactory {} // new non-generic base interface interface iwidgetfactory<twidget> : iwidgetfactory twidget : iwidget { } // generic constraints wind not specific, // still provide level of restriction // twidget can no longer used within method. twidgetfactory createfactory<twidgetfactory>() twidgetfactory : iwidgetfactory { return ... }
Comments
Post a Comment