Object identity

135 views
Skip to first unread message

Paul

unread,
May 20, 2012, 4:45:20 PM5/20/12
to scala-...@googlegroups.com
You can use the `eq` operator for checking for object identity with any AnyRef object in Scala.

Wouldn’t it be more elegant to rename this to `is`. This way, you could write more readable code

    if (a is b) ...

What do you think?

Regards, Paul

Rex Kerr

unread,
May 20, 2012, 4:52:02 PM5/20/12
to Paul, scala-...@googlegroups.com
This eq a really bad idea.

"A cow is a mammal."

Seems like "is" has as much to do with types than with individual identity, and certainly isn't a synonym for referential equality.

Besides which, introducing a new reserved word, which happens to be an extremely common word, for aesthetic reasons is a good way to cause a huge headache for people who used the word to mean something else.  There will be many such headaches.

  --Rex

etorreborre

unread,
May 20, 2012, 7:57:55 PM5/20/12
to scala-...@googlegroups.com, Paul
Yes, claiming a new "is" reserved word would be problematic for some people :-).

E.

Paul

unread,
May 21, 2012, 2:41:58 AM5/21/12
to scala-...@googlegroups.com, Paul
This sounds reasonable.

Eric Kolotyluk

unread,
May 21, 2012, 9:07:04 AM5/21/12
to scala-...@googlegroups.com
In some languages the keyword 'isa' is used - presumably in Scala you could use 'is' and 'isa' as two different keywords - but I am not advocating that.

I also find 'eq' a little annoying as for me 'it does not read well' in code. On the other hand, I would not be offended if there was some symbolic operator (chicken track) for 'these two objects are the same object, that is they are identical' such as '≡' so long as there were an easy way to insert it from a standard keyboard in the appropriate IDE. I think

   if a ≡ b then . . . else . . .

is perfectly clear (to me) and most readable, where 'eq' is a synonym for '≡'

We have a wonderful world of UTF-8 that we should use better, as there are just some mathematical symbols that are very clear and well understood, just so long as no-one gets carried away, for example using '⊨' instead of 'true' and '⊭' instead of false.

Cheers, Eric

Josh Suereth

unread,
May 21, 2012, 9:18:39 AM5/21/12
to Eric Kolotyluk, scala-...@googlegroups.com
a hasSameMemoryLocationAs b

Or with a pointless DSL:

a has same memory location as b

Verbosity FTW.


And an implementation for the confused:

object location
object same
implciit def betterEquals[A](a: A) = new {
  def has(x: same.type) = new {
    def memory(x: location.type) = new {
      def as[B](b: B) = a eq b

Jonas Bonér

unread,
May 21, 2012, 6:46:51 PM5/21/12
to Josh Suereth, Eric Kolotyluk, scala-...@googlegroups.com

LOL. Taken.

--
Jonas Bonér

Reply all
Reply to author
Forward
0 new messages