Re: Overriding == for strings

"Daniel Bonniot" <[email protected]> Fri, 15 Feb 2008 23:39:08 +0100
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
On Fri, Feb 15, 2008 at 8:52 PM, Dan Shryock <[email protected]> wrote:
> As a simple test, I am trying to override the == operator for string
>  comparison.  The problem that I am running into is that I see no way
>  to do a reference test once you override the method.  In some
>  languages I have seen an === operator that always does a strict
>  reference test.  Is there any way to do something similar in nice?

You can simply give a new name to the == operation:

override boolean `==`(String s1, String s2) = s1.equals(s2);

boolean same(Object o1, Object o2) = o1 == o2;

void main(String[] args) {
  let s1 = "a", s2 = "ba".substring(1);
  println(s1 == s2); // true
  println(same(s1, s2)); // false
}

Daniel

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/