c# - Allow templates to be inferred -
say i'm using external package storing graphs. bidirectionalgraph takes 2 templates: vertex , edge type:
var graph = new bidirectionalgraph<vertex, edge<vertex>>(); unfortunately, graph package doesn't allow edges radiating vertex in single line. instead, have provide ienumerable, populate results. can disrupt coding rhythm making tasks "loop through vertices successors of vertex x" take far code.
i wanted use .net's extensions add one-line solution graph class:
public static class graphextensions { public static ienumerable<tedge> incomingedges<tgraphsubtype, tvertex, tedge>(this tgraphsubtype graph, tvertex n) tgraphsubtype : bidirectionalgraph<tvertex, tedge> tedge : iedge<tvertex> { ienumerable<tedge> inputedgesforvertex; graph.trygetinedges(n, out inputedgesforvertex); return inputedgesforvertex; } } but when call graph.incomingedges(vertex), reason c# (.net version 4.5) can't infer template arguments, have say:
graph.incomingedges<graphthatinheritsfrombidirectionalgraph<vertextype,edgetype>,vertextype,edgetype>(vertex). not great improvement.
first, why can't template types estimated? have feeling has inheritance, don't understand. i'm used using c++, , reason feel gcc infer template types.
second, if can't prevented, correct design choice make graph class actual use, inherits bidirectionalgraph? seems waste have rewrite constructors, i'm sure you'd agree calling method explicit template types inelegant.
edit:
strangely, equivalent specification (below) does allow automatic inference of template types. so, though solves initial problem (adding functionality graph), i'd still understand.
public static class graphextensions { public static ienumerable<tedge> incomingedges<tvertex, tedge>(this bidirectionalgraph<tvertex,tedge> graph, tvertex n) tedge : iedge<tvertex> { ienumerable<tedge> inputedgesforvertex; graph.trygetinedges(n, out inputedgesforvertex); return inputedgesforvertex; } }
the first version of extension method able infer tgraphtype , tvertex not tegde, require inferring tedge type constraint:
where tgraphsubtype : bidirectionalgraph<tvertex, tedge> which c# compiler not (it not infer generic type parameters type constraints). don't know if there technical reason behind or wasn't implemented.
your updated version, on other hand, includes bidirectionalgraph<tvertex, tedge> parameter, example when call extension method on class like:
class agraph: bidirectionalgraph<avertex, anedge> { ... } ... var agraph = new agraph(); agraph.incomingedges(vertex); the compiler able examine type agraph , see there unique type bidirectionalgraph<avertex, anedge> in inheritance hierarchy, able infer tvertex , tedge.
note if parameter type igraph<tvertex, tedge> (instead of bidirectionalgraph<tvertex, tedge>) , agraph implemented multiple constructed types of generic interface, e.g.:
class agraph: igraph<avertex, anedge>, igraph<anothervertex, anotheredge> { ... } then type inference fail once again because can't tell if, example, tvertex avertex or anothervertex.
Comments
Post a Comment