I've been learning scala and I gotta say that it's a really cool language. I especially like its pattern matching capabilities and function literals but I come from a javascript, ruby background and one of my favorite patterns in those languages is the lazy function and method definition pattern. An example in javascript is
var foo = function() {
var t = new Date();
foo = function() {
return t;
};
return foo();
};
The same code with minor tweaks works in ruby where you just use the singleton object to redefine the method after the computation is performed. This kind of thing comes in really handy when expensive computation are involved and you don't know ahead of time if you are going to need the result. I know that in scala I can use a cache to simulate the same kind of result but I'm trying to avoid conditional checks and so far my experiments have returned negative results. Does anyone know if there is a lazy function or method definition pattern in scala?
Note: The javascript code is from Peter Michaux's site.