1

Topic: Circle links in

At me the program on C# also is eaten objects (plants and ) and eating objects ( and predators). I want to make such circuit of classes://I eaten by an animal, at which victim I public interface IEatableBy <TEater> where TEater: Animal <IEatableBy <TEater>> {void BeEatenBy (TEater animal);} public abstract class Animal <TVictim>//my victim is eaten by me where TVictim: IEatableBy <Animal <TVictim>> {///<summary>///a victim///</summary> protected HashSet <TVictim> Victims;} public class Plant: IEatableBy <Herbivore> {public void BeEatenBy (Herbivore animal) {}} public class Predator: Animal <Herbivore> {} public class Herbivore: Animal <Plant>, IEatableBy <Predator> {public void BeEatenBy (Predator animal) {}} the Problem that at such circuit  produces such errors: 1) at line public interface IEatableBy <TEater> underlines TEater and writes: Type "Assets. IEatableBy <TEater>" cannot be used as type parameter "TVictim" in the general-purpose type or a method "Animal <TVictim>". There is no conversion of the implicit link from "Assets. IEatableBy <TEater>" in "Assets.IEatableBy<Animal<Assets.IEatableBy<TEater>>>" 2) at line public abstract class Animal <TVictim>: MonoBehaviour underlines TVictim and writes: the Type "Animal <TVictim>" cannot be used as type parameter "TEater" in the general-purpose type or a method "IEatableBy <TEater>". There is no conversion of the implicit link from "Animal <TVictim>" in "Animal<Assets.IEatableBy<Animal<TVictim>>>" My questions: 1) Why there are such errors at compilation? My circuit with all it  breaks any common sense or logic (then how it is broken)? 2) If my circuit does not break common sense, why not ? A lack  ? That is theoretically in the future it can begin ? 3) How correctly to implement this my intention, but that it ? Many answer the third question: "what for to do these noodles both  and , anything in it it is not clear. Get rid of them and all earns". But why I here want to use  and such restrictions? It is obvious, that Predators could eat only , and  only plants. And that in a derivative class "Travojadnye"  protected HashSet <TVictim> Victims; contained only plants and the method public void BeEatenBy (Predator animal) at  accepted in parameter only predators. And by analogy to all other classes.

2

Re: Circle links in

Hello, gamburger, you wrote: G> My circuit with all it  breaks any common sense or logic (then how it is broken)? It is It seems that broken to-/kontravariantnost.  about keywords in/out in generic' in a context covariance/contravariance, in what direction the variance of input and output parameters of functions changes.

3

Re: Circle links in

Hello, Qbit86, you wrote: Q> Hello, gamburger, you wrote: G>> My circuit with all it  breaks any common sense or logic (then how it is broken)? Q> it is It seems that broken to-/kontravariantnost.  about keywords in/out in generic' in a context covariance/contravariance, in what direction the variance of input and output parameters of functions changes. I already tried to do interface parameter covariant or contravariant. public interface IEatableBy <in TEater> and public interface IEatableBy <in TEater> in a case with in changes nothing, in a case with out the error with TVictim remains, but the error with TEater disappears (but there is other error that in method BeEatenBy parameter TEater cannot be entering, but with it all is clear). If to consider the circuit on an example on an example of the Predator and Trevojadnogo: 1) public class Predator: Animal <Herbivore> hence 2) public abstract class Animal <Herbivore>//my victim is eaten by me where Herbivore: IEatableBy <Animal <Herbivore>> hence 3) public interface IEatableBy <Animal <Herbivore>> where Animal <Herbivore>: Animal <IEatableBy <Animal <Herbivore>>> we go reversely in : 4) public abstract class Animal <IEatableBy <Animal <Herbivore>>>//my victim is eaten by me where IEatableBy <Animal <Herbivore>>: IEatableBy<Animal<IEatableBy<Animal<Herbivore>>>> then we go further reversely in IEatableBy etc. That is here it is visible that 2) differs from 4), but my brain is too feeble, that most to understand, what exactly here not so. But I want to understand at last it since any more the first time I get to such noodles with . Perhaps who-thread understands that there happens, because of what not , and that it is possible to make to turn out  the code, thus that predators could not eat a plant.

4

Re: Circle links in

Hello, gamburger, you wrote: There and then  a recursion and I am not assured that the compiler can consult with it. Here there is an arguing of a similar case with the link to a blog of Lipperta where it assorts a similar case. And what for generally to fence such difficult design of classes?

5

Re: Circle links in

Hello, Sharov, you wrote: S> Hello, gamburger, you wrote: S> There and then  a recursion and I am not assured that the compiler can consult with it. If business was only in a recursion  should write about a recursion. But he was specific writes about that something somewhere is not resulted. S> Here there is an arguing of a similar case with the link to a blog of Lipperta where it assorts a similar case. About a cat who can be on friendly terms with evilDog, I read (but in my case, like as, not absolutely that situation, there under the link want to restrict for cats type parameter only to cats, but I do not want to restrict in any way parameter of type TVictim for Predators, I want, that TVictim could be any but that thus  Victims it has been strictly typified by type TVictim). S> And what for generally to fence such difficult design of classes? Matter is not that I try to complicate as much as possible a program without obtaining of any advantages, and that I first of all want to get of advantage (that the list of victims of Hitsnika always will consist only of Travojadnyh, and the list of victims of Travojadnogo will always consist of Plants, and also BeEatableBy at Travojadnogo always it will be caused only with the Predator, and in the Plant always only with Travojadnym, that is I want to typify strictly all) and as a result attempt all strictly to typify at me noodles with  turn out always. I think that should be possible to set such adjustments  and  that all was strictly typified and thus  if only  itself is not a barrier because from the point of view of logic I do not see here problems. Or, can, someone offers other circuit without such  but that all was strictly typified.

6

Re: Circle links in

Hello, gamburger, you wrote: G> I think that should be possible to set such adjustments  and  that all was strictly typified and thus  if only  itself is not a barrier because from the point of view of logic I do not see here problems. With  logicians and a problem, at you as a matter of fact the recursive determination - the abstract class depends on the interface, the interface depends from  a class.

7

Re: Circle links in

Hello, Sharov, you wrote: S> Hello, gamburger, you wrote: G>> I think that should be possible to set such adjustments  and  that all was strictly typified and thus  if only  itself is not a barrier because from the point of view of logic I do not see here problems. S> with  logicians and a problem, at you as a matter of fact the recursive determination - the abstract class depends on the interface, the interface depends from  a class. In a case . Dependences class Q1: Q2 {} class Q2: Q1 {}  writes: Cyclical dependence of base class includes "Q2" and "Q1" And in my case he writes at all that. But even if to assume, what a problem in a recursion then how to implement the circuit for such task? There is a Predator, Travojadnoe, the Plant. A predator and Travojadnoe should have the list of victims, since  the general it it is carried out in base class the Animal, and is necessary, that the Predator victims could have only Travojadnye, and Travojadnyh victims could have only Plants.  and Plants can be equally eaten  Predators and Travojadnymi, i.e. the general method BeEatenBy (Animal animal) is necessary. Thus that at Travojadnogo this method was such BeEatenBy (the Predator animal), and at the Plant such BeEatenBy (Travojadnoe animal). How to make such circuit?

8

Re: Circle links in

Hello, gamburger, you wrote: G> In a case . Dependences G> G> class Q1: Q2 G> {} G> class Q2: Q1 G> {} G> G>  writes: G> Cyclical dependence of base class includes "Q2" and "Q1" G> And in my case he writes at all that. As a matter of fact too most, but in other words. G> how to make such circuit? public interface IEatableBy <TEater> where TEater: Entity {void BeEatenBy (TEater animal);} public class Entity {} public class Animal: Entity {} public abstract class Animal <TVictim>//my victim is eaten by me where TVictim: IEatableBy <Entity> {///<summary>///a victim///</summary> protected HashSet <TVictim> Victims;} Or to remove restriction at the interface that is more logical. It is devoured with someone or something, well and it is fine.

9

Re: Circle links in

Hello, Sharov, you wrote: S> S> public interface IEatableBy <TEater> S> where TEater: Entity S> {S> void BeEatenBy (TEater animal); S>} S> public class Entity S> {S>} S> public class Animal: Entity {} S> public abstract class Animal <TVictim> S>//my victim is eaten by me S> where TVictim: IEatableBy <Entity> S> {S>///<summary> S>///victim S>///</summary> S> protected HashSet <TVictim> Victims; S>} S> Or to remove restriction at the interface that is more logical. It is devoured with someone or something, well and it is fine. Both cases do not approach under restriction that plants should be eaten only . If to remove restriction at the interface it is possible to set that the plant was eaten by plants. If to use the code above classes should be such public class Herbivore: Animal <Plant>, IEatableBy <Entity> {} public class Predator: Animal <Herbivore> {} public class Plant: IEatableBy <Entity> {} that is Herbivore and Plant both implement IEatableBy <Entity>, that is them eats Entity, but Entity is any animal, and it is necessary, that  was eaten only by predators, and plants only . S> Or to remove restriction at the interface that is more logical. It is devoured with someone or something, well and it is fine. Really in practice so always also becomes? But if there will be no normal typification I can be mistaken and transfer in the Plant as an eating Predator, as a result the program will not work, as it is necessary. In general, somehow strange, really   it is so bad, what there it is impossible to typify in any way the data in this case?

10

Re: Circle links in

Hello, gamburger, you wrote: G> At me the program on C# also is eaten objects (plants and ) and eating objects ( and predators). I want to make such circuit of classes: On  to you to write already advised? Such  you do not make recursive in C#. For a recursion any its explicit stop is necessary - and how you set it in C#?

11

Re: Circle links in

Hello, gamburger, you wrote: G> At me the program on C# also is eaten objects (plants and ) and eating objects ( and predators). I want to make such circuit of classes: did not understand than "the simple" approach does not arrange ("can eat"): public interface CanEat <T> {void eat (T something);} public class Plant {} public class Herbivore: CanEat <Plant> {public void eat (Plant thing) {}} public class Predator: CanEat <Herbivore> {public void eat (Herbivore thing) {}} Well or, on the contrary ("can be eaten") public interface CanBeEatenBy <T> {void beEatenBy (T something);} public class Predator {} public class Herbivore: CanBeEatenBy <Predator> {public void beEatenBy (Predator something) {}} public class Plant: CanBeEatenBy <Herbivore> {public void beEatenBy (Herbivore something) {}}