Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Proposed templated opEquals #1087

Closed
wants to merge 1 commit into from
Closed

Conversation

marler8997
Copy link
Contributor

Templated opEquals.

@marler8997 marler8997 force-pushed the opEquals branch 3 times, most recently from 157b0ee to adf0786 Compare January 6, 2015 18:24
@schveiguy
Copy link
Member

Without committing any opinion whether or not this is appropriate, you also need to duplicate all the code to object.di.

@MartinNowak
Copy link
Member

Without committing any opinion whether or not this is appropriate, you also need to duplicate all the code to object.di.

Right, because the compiler need to see the code to instantiate a template.

@marler8997
Copy link
Contributor Author

Thanks @schveiguy , I forgot about object.di. There's been talk in the forums about templating opEquals so I thought creating a PR would help continue the discussion and hopefully we'll come to a solution.

@@ -253,6 +259,16 @@ class TypeInfo
auto ti = cast(const TypeInfo)o;
return ti && this.toString() == ti.toString();
}
bool opEquals(const TypeInfo ti)
Copy link
Member

Choose a reason for hiding this comment

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

final?

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 thought about final but since the generic opEquals wasn't final I just followed suite. I guess it really depends on if anyone would want to override the opEquals method. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

TypeInfo's derivatives are all defined in druntime. If none of them define opEquals, then I think it's safe to make this final. In any case, it's an implementation detail that can be changed later.

@marler8997 marler8997 force-pushed the opEquals branch 2 times, most recently from bcd73ed to 959e106 Compare January 6, 2015 20:25
@marler8997
Copy link
Contributor Author

I'm not sure I quite understand why this is failing. From digging a little bit, it looks like templates inside druntime are not visible to code outside druntime...is this correct?

@schveiguy
Copy link
Member

You are messing with a very fundamental piece of the runtime. Errors get very very weird here.

In answer to your question -- templates inside druntime are available to outside code, as long as the template is visible. Otherwise, all the templates in object.di would never work!

Note, the compiler can do weird things assuming certain things about druntime. What those are, and whether they are causing this error, I'm not sure.

@schveiguy
Copy link
Member

whether they are causing this error,

I'm actually quite sure that's the issue. I think the compiler calls object.opEquals(Object, Object) directly (i.e. just outputs the correct mangled symbol). I think you change the signature so it's failing.

You may need a dmd pull to get this to work...

@marler8997
Copy link
Contributor Author

I think I've figured some things out. It looks like templates inside object_.d are not visible but templates inside object.di are (even when I put the signature of the template inside object.di). After making the changes...there are some errors in phobos...is there a way to tie two PR's together for druntime and phobos?

@schveiguy
Copy link
Member

is there a way to tie two PR's together for druntime and phobos?

Not currently. why not try building locally and fix the issues? Perhaps it's something that would benefit phobos all by itself.

@marler8997
Copy link
Contributor Author

I've made the changes in phobos to fix the build. I don't know why these changes were necessary but for some reason they were. I've made a PR here: dlang/phobos#2848

@marler8997
Copy link
Contributor Author

Is there a way to mark this PR as dependent on dlang/phobos#2848 ?

@marler8997
Copy link
Contributor Author

For some reason, when I remove the generic opEquals(Object,Object) function, it causes structs that do not have an opEquals method to not be able to be used as keys for associative arrays. I get the error:

Error: AA key type <typename> does not have 'bool opEquals(ref const <typename>) const'

I get this error for Tid (in concurrency.d) and in Typedef (in typecons.d).

}

bool opEquals(Object lhs, Object rhs)
bool opEquals(T,U)(T lhs, U rhs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it would be better to restrict T and U to class types. You wouldn't need all the static ifs anymore.

Using a template specialization:

bool opEquals(T : const Object, U : const Object)(T lhs, U rhs)

@aG0aep6G
Copy link
Contributor

aG0aep6G commented Jan 6, 2015

Something I think you haven't considered: Currently, a non-const opEquals is called even on const/immutable objects. That's what the "A hack for the moment." overload did. As far as I can see you're breaking that here. I don't know how important it is to keep that problematic behaviour alive, but it's definitely something to be considered.

@aG0aep6G
Copy link
Contributor

aG0aep6G commented Jan 6, 2015

There's been a pull request by @jmdavis for the same thing: #459. It's been blocked by issue 12537.

@marler8997
Copy link
Contributor Author

@aG0aep6G Thanks for referencing that. I looked for a PR for opEquals but didn't find one.

@marler8997
Copy link
Contributor Author

Rebased PR from @jmdavis (#459). Hopefully the dmd tests are passing now...we'll see.

@marler8997
Copy link
Contributor Author

dmd tests still fail, this PR should be closed since it is a duplicate of #459 .

@marler8997 marler8997 closed this Jan 7, 2015
@marler8997 marler8997 deleted the opEquals branch January 7, 2015 00:31
@marler8997 marler8997 restored the opEquals branch January 7, 2015 14:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants