D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11557 - Add ability to compare `const`/`immutable` classes
Summary: Add ability to compare `const`/`immutable` classes
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-11-19 13:08 UTC by Denis Shelomovskii
Modified: 2024-12-13 18:14 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Denis Shelomovskii 2013-11-19 13:08:50 UTC
Currently because of Issue 1824 `opEquals` and `opCmp` aren't `const`. It's bad but works if these functions are implemented carefully.

Current dmd rejects `opCmp` call:
---
const Object o1, o2;
assert(o1 == o2); // ok, calls `opEquals`
assert(o1 <= o2); // Error: mutable method object.Object.opCmp is not callable using a const object
---

As a result e.g. `std.typecons.Tuple`-s with classes don't have `opCmp` and fails with "TypeInfo.compare is not implemented" `Error` if used as an associative array key. Error at runtime is really nasty.
Comment 1 Denis Shelomovskii 2013-11-19 13:14:09 UTC
A workaround for `std.typecons.Tuple` regression:
https://github.com/D-Programming-Language/phobos/pull/1707
Comment 2 Kenji Hara 2013-11-20 01:26:06 UTC
(In reply to comment #0)
> Currently because of Issue 1824 `opEquals` and `opCmp` aren't `const`. It's bad
> but works if these functions are implemented carefully.
> 
> Current dmd rejects `opCmp` call:
> ---
> const Object o1, o2;
> assert(o1 == o2); // ok, calls `opEquals`
> assert(o1 <= o2); // Error: mutable method object.Object.opCmp is not callable
> using a const object
> ---
> 
> As a result e.g. `std.typecons.Tuple`-s with classes don't have `opCmp` and
> fails with "TypeInfo.compare is not implemented" `Error` if used as an
> associative array key. Error at runtime is really nasty.

Why this issue is marked as "regression"? As far as I know, the code you shown had never worked correctly.
Comment 3 Denis Shelomovskii 2013-11-20 07:31:13 UTC
(In reply to comment #2)
> Why this issue is marked as "regression"? As far as I know, the code you shown
> had never worked correctly.

Associative arrays use `TypeInfo.compare` and `std.typecons.Tuple` has its `opCmp` with by element comparison for a long time. As `Tuple`-s stopped working as AA keys (it worked at least a year ago) I decided it was a compiler change. If it's not please remove the REGRESSION status.
Comment 4 Kenji Hara 2013-11-21 07:30:09 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > Why this issue is marked as "regression"? As far as I know, the code you shown
> > had never worked correctly.
> 
> Associative arrays use `TypeInfo.compare` and `std.typecons.Tuple` has its
> `opCmp` with by element comparison for a long time. As `Tuple`-s stopped
> working as AA keys (it worked at least a year ago) I decided it was a compiler
> change. If it's not please remove the REGRESSION status.

See my comment in your PR. To me the old worked behavior was essentially wrong, and I think it should be kept broken until class const correctness will be fixed.

https://github.com/D-Programming-Language/phobos/pull/1707#issuecomment-28992632
Comment 5 Denis Shelomovskii 2013-11-24 00:56:24 UTC
Filed Issue 11588 for inconsistency in abilities to compare `const`/`immutable` classes and Issue 11591 for Phobos regression.

This issue is a request to add ability to compare `const`/`immutable` classes just like currently allowed testing for equality. It will fix Issue 11588 if accepted.
Comment 6 dlangBugzillaToGithub 2024-12-13 18:14:06 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17627

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB