« Return to Thread: Some 2.8 collection ideas that arose out of IRC discussion about hash codes

Some 2.8 collection ideas that arose out of IRC discussion about hash codes

by David MacIver :: Rate this Message:

Reply to Author | View in Thread

The bickering about this in scala-debate overflowed into #scala. The
following two concrete ideas were suggested in the course of the
discussion. I think both are interesting, independently of what is
done about mutable hash codes:


1. A freeze method on mutable collections. This would cause the
collection to throw exceptions on attempts to mutate it. An example
use case (if mutable hash codes were made to work) would be to freeze
mutable hash keys before using the collection as a key in a hash
table, thus preventing your keys from accidentally being mutated. I'm
sure there are others.

One question is whether there should also be a corresponding "thaw"
method to unfreeze it. I think there should, but I don't feel that
strongly about it. On the one hand the lack of it allows for a much
stronger this will never be mutated again guarantee. On the other hand
the major use case for it I see is to be able to specify regions where
you want to enforce that it will never be mutated.

Alternative: As well as providing a freeze method you can provide a
whileFrozen method (or something like that) with the signature

def whileFrozen[T](action : =>T) : T = { ... }

which freezes the object only for the duration of the call. It's
probably worth providing this even if there is a thaw.


2. Parameterized hashing. This is actually something I've wanted for a
while. Have collections which depend on hashing take an implicit
Hasher parameter where

trait Hasher[-T]{
  def hashOf(x : T) : Int;
  def areEqual(x : T, y : T) : Boolean;
}

this is then used to define the hashing behaviour of the collection,
much like Ordering. However there would be an implicit Hasher[Any]
available, so it would always succeed, but could specialise in some
cases.

 « Return to Thread: Some 2.8 collection ideas that arose out of IRC discussion about hash codes