Sheding some light on generics and that variance thing...

Sheding some light on generics and that variance thing...
http://delphisorcery.blogspot.com/2014/10/generics-and-variance.html

Comments

  1. What I did not mention there is that in Spring4D all interfaced collection types have a guid so you can do something like this (which I suggest you should be very careful about since you are violating variance here) - entities and persons are both IList:

    entities := persons as IList;

    However as I mentioned in the article you should take the correct type that would be covariant, like - entities is an IReadOnlyList:

    entities := persons as IReadOnlyList;

    That way you make sure you are respecting covariance and not doing any operation that would be against that (well to be honest removing elements would not be but is not supported by a read only list for obvious reasons).

    ReplyDelete

Post a Comment