logo      
Custom Search

Re: more liberal Some.equals Re: Maps in scala

Date: December 28, 2006
From: Jay Sachs <jay@xxxxxxxxx>

In-reply-to: <ac0ce270612280906j78d40a94s32c4d936109c3956@xxxxxxxxxxxxxx>
References: <ac0ce270612280749p3e027f81pbbe737e45181afca@xxxxxxxxxxxxxx> <ECBF64C0-9F80-4614-98B9-F4A0D100978F@xxxxxxxxx> <ac0ce270612280906j78d40a94s32c4d936109c3956@xxxxxxxxxxxxxx>

On Dec 28, 2006, at 12:06 PM, Burak Emir wrote:

On 12/28/06, Jay Sachs <jay@xxxxxxxxx> wrote:
On Dec 28, 2006, at 10:49 AM, Burak Emir wrote:

> do you think it would make sense to define Some.equals method such
> that Some(x) == x?

But then wouldn't Some(None) == None ?

Good point. One could make a special case for None.

Options of options are quite rare, and a big obstacle to optimization of option types, but since we have them, it would surely be very confusing to have Some(None) == None. Also Some(Some (x)) should not be == Some(x). So here's what is meant.

case class Some[a](x:a) extends Option[a]{
  def equals(y:Any) = y match {
     case None => false
     case Some(z) => x == z
     case z => x == z
  }
}

It's just a proposal, if nobody ever wished for this thing, it's probably not worth including.

I think it's difficult if not impossible to get the semantics right. You've eliminated Some(Some(x)) == Some(x), but what if the nested "Some" is inside a list:

  Some( List(Some(3)) : List[Any] ) == List(3) : List[Any] )

Per your definition above, this evaluates to

  List(Some(3)) == List(3)

which recursively will invoke Some.== and return true.





Custom Search
home | non blog view