Skip to content

Conversation

marler8997
Copy link
Contributor

I'm not sure why the Address classes do not have opEquals implemented. I've added it to the InternetAddress class. If this get's "approved" then I can add the other opEquals methods to the other Address classes.

* Examples:
* --------------
* InternetAddress addr1,addr2;
* if (addr1 == addr2) { }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm.. won't this example segfault due to a null this? I forgot whether this was ever fixed (or ever will be).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and it seems to work. http://dlang.org/operatoroverloading.html

If a and b are both class objects, then the expression is rewritten as:
.object.opEquals(a, b)
and that function is implemented as:

bool opEquals(Object a, Object b)
{
    if (a is b) return true;
    if (a is null || b is null) return false; // NULL HANDLED HERE
    if (typeid(a) == typeid(b)) return a.opEquals(b);
    return a.opEquals(b) && b.opEquals(a);
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool so it was fixed, great!

@ghost
Copy link

ghost commented Jan 4, 2015

Auto-merge toggled on

ghost pushed a commit that referenced this pull request Jan 4, 2015
Added opEquals to InternetAddress
@ghost ghost merged commit 79d5b09 into dlang:master Jan 4, 2015
@ghost
Copy link

ghost commented Jan 4, 2015

Thanks, looks good.

override bool opEquals(Object o) const
{
auto other = cast(InternetAddress)o;
return other && this.sin.sin_addr.s_addr == other.sin.sin_addr.s_addr &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is testing other for null really necessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other will be null if o wasn't an InternetAddress.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good thing to discuss. It depends on how opEquals is supposed to be used. If it's valid to pass other non-InternetAddress objects then it is necessary. If not, then we could use a contract to validate that o is of type InternetAddress. However, I plan on submitting an enhancement request to change the '==' operator to use a typed opEquals instead of the generic one, which would mean we wouldn't need to cast the Object parameter cause we could just use an InternetAddress directly.

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

Successfully merging this pull request may close these issues.

3 participants