Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:36:45+00:00 2026-06-10T05:36:45+00:00

Take a look at this simple class I’m building as the base to store

  • 0

Take a look at this simple class I’m building as the base to store the results of string matching algorithms:

/** Match of a single pattern in full to a single text. */
class Match {
    uint Tpos;

    this(in uint Tpos) { this.Tpos = Tpos; }

    override string toString() {
        return text("Match: Text@",Tpos);
    }
}

Here’s where things get weird:

auto m1 = new Match(1), m2 = new Match(1);
writeln(m1.toHash());
writeln(m2.toHash());
writeln(m1 == m2);

prints

4464528
4464512
false

I see no reason why these two objects should not be considered equal by default. I suppose I could write a custom toHash() and opEquals() function, but that seems like overkill. According to Andrei Alexandrescu’s book on the D programming language (great book!), “By default, the hash is computed by using the bitwise representation of the object.” Any ideas out there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:36:46+00:00Added an answer on June 10, 2026 at 5:36 am

    From the source code (dmd2/src/druntime/src/object_.d):

    class Object
    {
    /* snip */
        /**
         * Compute hash function for Object.
         */
        hash_t toHash() @trusted nothrow
        {   
            // BUG: this prevents a compacting GC from working, needs to be fixed
            return cast(hash_t)cast(void*)this;
        }
    /* snip */
        /**
         * Returns !=0 if this object does have the same contents as obj.
         */
        equals_t opEquals(Object o)
        {
            return this is o;
        }
    }
    

    So the answer is simply that’s the way the code is written – they do an identity check rather than a content check. Why is it this way? I don’t really know, but my guess is it was simple to write originally and works well enough that nobody has bothered to come back to it and change it.

    On the newsgroup, there’s been some discussion of removing these functions from Object entirely, so if you want == on your classes, you’ll have to implement something. But the time it takes for newsgroup talk to become action when it comes to things like this is usually pretty long. And they might change their minds.

    The best way of using class equality currently and probably in the foreseeable future is to write your own opEquals method in the class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take a look at this code: public class Test { public static void main(String...
Take a look at this table: <table cellpadding=0 cellspacing=0 class=order_form> <tr> <th>Amount</th> <th>Desc</th> <th>Price</th>
Please take a look at my simple three-methods Service class that streams audio and
This is probably really simple but i cant figure it out. Take a look
Please take a look at the following simple code: class Foo { public: Foo(){}
Take this very simple form for example: class SearchForm(Form): q = forms.CharField(label='search') This gets
take look at this: http://www.templatemonster.com/demo/35403.html In this template you can click on the image
Take a look at this example here: http://denise.brixwork.com/showlisting/1/8297-Valley-Drive-Alpine-Meadows-Whistler-denise-brown-real-estate And the red tables under Specifications
Take a look at this: http://thebekker.dk/_skole/GFeksamen/ You can see the 2nd menu item show
Take a look at this with IE or Chrome and notice the yellow block:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.