Nice, I've also thought a lot about cached values in the same way. In the past I implemented a meta-programmed system in Ruby to deal with cached values so they would be easy to use and automatically dropped when other related state changed... and I really started to think about the concept of "derived state". I feel it should be something implemented in common programming languages. I believe it could be extremely helpful. Does someone know if this exists in some language?
I don't know if there's any other tricky implementation part, but from what I've seen you could simply define something like this:
struct Human:
name String
birth Date
derived age Integer
function derive age:
return (time.Now() - self.birth).Years().Floor()
And the compiler should have everything it needs. Sure, this example is pretty annoying because time changes all the time, so it doesn't look like you can cache much with such a naive approach, but I suck at examples (surely people working on languages could come up with more interesting approaches, like adding ways to schedule the cached value to be preferably kept until X time later or whatever).
I don't know if there's any other tricky implementation part, but from what I've seen you could simply define something like this:
And the compiler should have everything it needs. Sure, this example is pretty annoying because time changes all the time, so it doesn't look like you can cache much with such a naive approach, but I suck at examples (surely people working on languages could come up with more interesting approaches, like adding ways to schedule the cached value to be preferably kept until X time later or whatever).