Overriding == for strings

View: New views
3 Messages — Rating Filter:   Alert me  

Overriding == for strings

by Dan Shryock :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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?

Dan

-------------------------------------------------------------------------
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/
_______________________________________________
Nice-info mailing list
Nice-info@...
https://lists.sourceforge.net/lists/listinfo/nice-info

Re: Overriding == for strings

by Daniel Bonniot-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Feb 15, 2008 at 8:52 PM, Dan Shryock <dan.shryock@...> 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/
_______________________________________________
Nice-info mailing list
Nice-info@...
https://lists.sourceforge.net/lists/listinfo/nice-info

Re: Overriding == for strings

by Dan Shryock :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Very cool, thanks.

On Fri, Feb 15, 2008 at 2:39 PM, Daniel Bonniot <bonniot@...> wrote:

>
> On Fri, Feb 15, 2008 at 8:52 PM, Dan Shryock <dan.shryock@...> 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/
>  _______________________________________________
>  Nice-info mailing list
>  Nice-info@...
>  https://lists.sourceforge.net/lists/listinfo/nice-info
>

-------------------------------------------------------------------------
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/
_______________________________________________
Nice-info mailing list
Nice-info@...
https://lists.sourceforge.net/lists/listinfo/nice-info
LightInTheBox - Buy quality products at wholesale price