Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

== returns false for apparently equal strings #94

Closed
quanglewangle opened this issue Dec 30, 2017 · 2 comments
Closed

== returns false for apparently equal strings #94

quanglewangle opened this issue Dec 30, 2017 · 2 comments
Labels

Comments

@quanglewangle
Copy link
Collaborator

namespace TRYOUT is
use System;
use Generic;

class Main is

// class Main is

    init() is
        var s1 = "abcd";
        var s2 = "b";

        IO.Std.out.println ("substring:" + s1.substring(1,2) + " s2: " + s2);

        if s1.substring(1,2) == s2 then
            IO.Std.out.println("hit");
        else
            IO.Std.out.println(">" + s1.substring(1,2) + "< should be equal to >" + s2 + "< but isn't");
        fi
    si
si

si

@quanglewangle
Copy link
Collaborator Author

Oops! should have been using =~

@degory
Copy link
Owner

degory commented Jan 1, 2018

Behaviour of == is similar to Java - for reference types it compares for reference equality only.

=~ is the overridable equality operator, and is defined by the L library for strings, returning true if the strings represent exactly equal sequences of bytes.

Only=~ can be defined, x !~ y returns, by definition, !(x =~ y). This behaviour cannot be overridden.

=~ and !~ are defensive against nulls and will never call the user operator method with a null argument. The compiler generates code around uses of =~ and !~ such that:

null =~ null
null !~ anything-non-null
anything-non-null !~ null'

without actually calling =~, for both constant and run-time nulls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants