Creating circular generic references

Circular generic references are indeed possible. Java Generics and Collections includes several examples. For your case, such a specimen would look like this: public interface P2PNetwork<N extends P2PNetwork<N, C>, C extends P2PClient<N, C>> { void addClient(C client); } public interface P2PClient<N extends P2PNetwork<N, C>, C extends P2PClient<N, C>> { void setNetwork(N network); } class TorrentNetwork … Read more

How to use @JsonIdentityInfo with circular references?

It seems jackson-jr has a subset of Jackson’s features. @JsonIdentityInfo must not have made the cut. If you can use the full Jackson library, just use a standard ObjectMapper with the @JsonIdentityInfo annotation you suggested in your question and serialize your object. For example @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property=”@id”) public class A {/* all that good stuff */} … Read more

How to restore circular references (e.g. “$id”) from Json.NET-serialized JSON?

The answers given almost worked for me, but the latest version of MVC, JSON.Net, and DNX uses “$ref” and “$id”, and they may be out of order. So I’ve modified user2864740’s answer. I should note that this code does not handle array references, which are also possible. function RestoreJsonNetReferences(g) { var ids = {}; function … Read more

How to create a circularly referenced type in TypeScript?

The creator of TypeScript explains how to create recursive types here. The workaround for the circular reference is to use extends Array. In your case this would lead to this solution: type Document = number | string | DocumentArray; interface DocumentArray extends Array<Document> { } Update (TypeScript 3.7) Starting with TypeScript 3.7, recursive type aliases … Read more

What lifetimes do I use to create Rust structs that reference each other cyclically?

It is not possible to create cyclic structures with borrowed pointers. There is not any good way of achieving cyclic data structures at present; the only real solutions are: Use reference counting with Rc<T> with a cyclic structure with Rc::new and Rc:downgrade. Read the rc module documentation and be careful to not create cyclic structures … Read more

Circular references in Javascript / Garbage collector

Any half-decent garbage collector will handle cycles. Cycles are only a problem if you do naive reference counting. Most garbage collectors don’t do ref-counting (both because it can’t handle cycles, and because it’s inefficient). Instead, they simply follow every reference they can find, starting from “roots” (typically globals and stack-based variables), and mark everything they … Read more