Why doesn't scala have easier support for Equals? -


reading martin's book, chapter equality object equality chapter 1 may notice implementing equals in scala (as in other language) not straightforward. scala extremely powerful , agile, , cannot believe not simplify things bit. know scala generates proper equals case classes, wonder why couldn't generate simplifications normal classes?

to show point, wrote example of how see should like. has flaws, , had use classtag know wrong such basic thing equals due performance (any tip how without classtag?), thinking scala can generate proper equals case classes, i'd should able generate proper code normal classes, giving developer provides key should used compare objects.

trait equality[t] extends equals {   val ttag: classtag[t]   def key: seq[any]    def canequal(other: any): boolean = other match {     case that: equality[_] if that.ttag == ttag => true     case _ => false   }    override def equals(other: any): boolean =    other match {      case that: equality[t] => canequal(that) && key == that.key      case _ => false    }    override def hashcode = key.foldleft(1)((x, y) => 41 * x + y.hashcode) } 

then can use this:

class point(val x: int, val y: int)(implicit val ttag: classtag[point]) extends equality[point]{     override def key: seq[any] = seq(x, y) } 

i'm not classtags, might have made wrong, seems working. still that's not asking - want know there serious reasons, why scala not simplify implementing equality checks?

it interesting idea, , can see comment scalaz has this. i'm not sure have complete answer, factors consider are:

case classes bit unique in you're not supposed inherit them. assuming they're not sub-classed, canequal isn't required.

there's elegant idea '+' in scala function not 'language feature.' it's not better have language feature instead of library (and there number of utilities in java implementing hashcode/equals). existing "equals" method isn't language feature, it's method on parent class object, inherited java.

scala still need play-nice java, , 1 barrier radically changing how equals works. interface requirements can't imposed on existing java classes scala might want inherit from.

when consider syntax might if language feature, i'm not sure eliminated or changed.
example, developer still has specify key suggest. also, able support, example, anonymous subclasses , trait mix-ins canequal won't change, language feature you'd still need explicitly define class tag.

maybe analysis more interesting if provide what, syntactically, might if language feature instead of helper library. might missing aspects of how work.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -