Can't overload operators (==, !=, +) by mapped class of NSString on iOS

Try following EUnit test cases:

public class OperatorTest : Test
{
    public void Test1()
    {
        var a = new TestClass();
        Assert.IsFalse(a==a);                  // passed
        Assert.IsTrue(a!=a);                  // passed
    }

    public void Test2()
    {
        var a = new TestClassMapped();
        Assert.IsFalse(a==a);                  // failed
        Assert.IsTrue(a!=a);                    // failed
    }
}

public class TestClass : Foundation.NSString{
    public static bool operator == (TestClass a, TestClass b) {
        return false;
    }
    public static bool operator != (TestClass a, TestClass b) {
        return true;
    }
}

public __mapped class TestClassMapped => Foundation.NSString{
    public static bool operator == (TestClassMapped a, TestClassMapped b) {
        return false;
    }
    public static bool operator != (TestClassMapped a, TestClassMapped b) {
        return true;
    }
}

I thought maybe it’s something wrong in RO C# compiler, then I try to simplify String.pas codes in Sugar to see if there’s no problem in Oxygene:

interface type OxygeneClass = public class mapped to Foundation.NSString public class operator Equal(Value1, Value2: OxygeneClass): Boolean; class operator NotEqual(Value1, Value2: OxygeneClass): Boolean; end;

implementation

class operator OxygeneClass.Equal(Value1: OxygeneClass; Value2: OxygeneClass): Boolean;
begin
exit false;
end;

class operator OxygeneClass.NotEqual(Value1: OxygeneClass; Value2: OxygeneClass): Boolean;
begin
exit true;
end;

end.

Unfortunately Test3 also failed:

    public void Test3()
    {
        var a = new OxygeneClass();
        Assert.IsFalse(a==a);       // Failed
        Assert.IsTrue(a!=a);         // Failed
    }

Here’s my test project:
ElementTest.zip (124.7 KB)

Thanks, logged as bugs://72402

So there’s actually a good reason for this. “String” == “String” has special rules and always uses the underlying String logic to do the comparison (which I noticed had a bug for null values as oyu said in the other thread). I’ll add some logic to disallow overriding these operators in mapped string classes.

There’s no need to override == and != actually, so disallowing the behaviors is ok.
But don’t forget the +(add) operator is also unable to be overrided for iOS, which is necessary if I want to wrap my own String class.
Without an overrided + operator, following codes can’t work on iOS:

public static class Program
{
	public Int32 Main(String[] args)
	{
		CustomString a = "asdf";
                (a+"xxx").doSth();      // can't compile
	}
}

public __mapped class CustomString => Foundation.NSString{

    public static CustomString operator + (CustomString a, CustomString b) {
        return ((string)a)+((string)b);
    }

    public void DoSth(){
    }
}

hrmm you are right about that, I’m going to see if I can do this somehow without breaking anything.

bugs://72402 got closed with status fixed.