Is there any difference between:
public interface IPathfinding<T> where T : IPathfindingEntity
{
IEnumerable<T> GetShortestPath(IPathfindingUtils<IGraphNode<T>> utils);
}
and:
public interface IPathfinding<T> where T : IPathfindingEntity
{
IEnumerable<T> GetShortestPath(IPathfindingUtils<IGraphNode<IPathfindingEntity>> utils);
}
I'm scratching my head as intuitively it doesn't seem like there should be yet I am receiving compiler error in example #1:
IGraphNode<T> cannot be used as parameter T in the generic type IPathfindingUtils<T>. There is no implicit reference conversion from IGraphNode<T> to IGraphNode<IPathfindingEntity>
I haven't changed the definition of IGraphNode<T>
or IPathfindingUtils<T>
between trying to compile the 2 examples, here are the definitions for those interfaces:
public interface IPathfindingUtils<T> where T : IGraphNode<IPathfindingEntity>
{
float Heuristic(T from, T to);
IEnumerable<T> CreatePath(T from);
}
public interface IGraphNode<T> : INode<T>, IComparable<IGraphNode<T>> where T : IComparable<T>
{
IList<float> Costs { get; }
}
IGraphNode requires T to be an IComparable.
So you will want your code to be the following:
public interface IPathfinding<T> where T : IPathfindingEntity, IComparable
{
IEnumerable<T> GetShortestPath(IPathfindingUtils<IGraphNode<T>> utils);
}
Never mind, I've rubber ducked myself and realized what the problem is.
For posterity:
In example #1, the constraint on T
is IPathfindingEntity
. IGraphNode<T>
is not constrained against IPathfindingEntity
and so IPathfindingUtils<IGraphNode<T>> utils
is not valid since T
in this case is IPathfindingEntity
and that would break liskov substitution against IGraphNode<T>
constraint which is not fullfilled!
So solution is to just make IPathfinding specifiy IGraphNode constraint in method definition as per example #2?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.