What I’ve been up to on 2008-07-22
July 22nd, 2008 by twitterbot- is wrestling with cyclic reference errors, and feels like a cyclic reference error himself. #
Lazy vals were an excellent addition to Scala 2.6.0. They allow you to defer calculation of a value until it is actually needed, after which the calculated value is cached.
Unfortunately, this doesn’t play so well with another wonderful Scala feature — implicit parameters. It’s often the case that you’ll want to defer resolution of an implicit until a method call, rather than in the scope of object creation. But lazy values cannot take any parameters, implicit or otherwise — so it seems you’re stuck.
Memoized to the rescue! This is a simple helper class that takes care of the caching of a result. Where you might have had the following method with an implicit parameter:
def foo(implicit bar: Bar): ResultType = { ... }
you instead write the following:
private var _foo = Memoized[ResultType] def foo(implicit bar: Bar) = _foo or { ... }
(complete source below the fold) Read the rest of this entry »
|
Copyright 2007 Aaron Harnly. Contact me by leaving a comment, or at (myfirstname)@harnly.net.
Entries (RSS) and Comments (RSS).19 queries. 1.173 seconds. |