Skip to content

Instantly share code, notes, and snippets.

@ohsitab
Created August 22, 2010 20:28
Show Gist options
  • Save ohsitab/544243 to your computer and use it in GitHub Desktop.
Save ohsitab/544243 to your computer and use it in GitHub Desktop.
// このようなクラスがあったとき
class T(prev: => T, next: => T)
// それぞれに変数を用意する場合は、このようにすれば循環参照を作成できるが
lazy val a: T = new T(c, b)
lazy val b: T = new T(a, c)
lazy val c: T = new T(b, a)
// このようなかんじに不定個の要素で循環参照を作成したい場合はどうすればよいだろうか
def f(n: Int) = (1 to n).foldLeft(null:T)((ref, _) => new T(ref, null))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment