“Turtles all the way down” is a concept that Java handles very nicely:
public class Turtle { Turtle prevTurtle; public Turtle(Turtle prevTurtle) { if (prevTurtle == null) { throw new RuntimeException("It's turtles all the way down."); } this.prevTurtle = prevTurtle; } }
(Probably there should be a special FiniteTurtleException
, but I wanted to keep the code compact.)
Similarly for other circular references:
class Chicken { Egg cameFrom; public Chicken(Egg hatchedFrom) { cameFrom = hatchedFrom; } } class Egg { Chicken cameFrom; public Egg(Chicken mother) { cameFrom = mother; } }
You can do it in C-like languages, but you need to be a little clever in your header files, which takes some of the fun out of it.