On Wednesday, June 16, 2004, 9:18:24 PM, Robert wrote:

>> And I guess one could write their own #to_s or #inspect in particular
>> cases if it were a problem for them.

> The cleanest might be to introduce something similar to C++'s keyword
> 'mutable', which allows instance variables to be changed even if the
> container is const (aka frozen).  But I don't like this approach because
> things soon get messy.  Constness is difficult to get right (not so much
> for the language but rather for users of the language) IMHO.

I had a thought about a solution today.  Let's just say that Date#to_s
is broken when the object is frozen because it's modifying internal
state for caching purposes.  Well, why not outsource the caching bit
to a different class.  For example (psuedo-code):

  class Date
    def to_s
      DateCache.to_s(self) {
        # Calculate the string rep here.  This code
        # is only called if necessary to populate the
        # cache for the first time.
      }
    end
  end

Then again, you can just store the cache info in a class variable.

Not that I've scrutinised the code or anything.

Gavin