diff options
Diffstat (limited to 'stage1/Cache.scala')
-rw-r--r-- | stage1/Cache.scala | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/stage1/Cache.scala b/stage1/Cache.scala new file mode 100644 index 0000000..6e6b9eb --- /dev/null +++ b/stage1/Cache.scala @@ -0,0 +1,14 @@ +package cbt +/** +Caches exactly one value +Is there a less boiler-platy way to achieve this, that doesn't +require creating an instance for each thing you want to cache? +*/ +class Cache[T]{ + private var value: Option[T] = None + def apply(value: => T) = this.synchronized{ + if(!this.value.isDefined) + this.value = Some(value) + this.value.get + } +} |