Make objects.opEquals a template?
ketmar via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jul 13 02:10:41 PDT 2015
On Mon, 13 Jul 2015 06:49:09 +0000, Yuxuan Shui wrote:
> The main reason here is to use '==' in @safe code:
>
> class A {
> override @safe bool opEquals(Object o) {
> return cast(A)o !is null;
> }
> }
> @safe void main() {
> A a = new A;
> A b = new A;
> a == b;
> }
>
> This doesn't compile in current D, because objects.opEquals takes two
> Object, and Object.opEquals is not @safe.
>
> Why can't we have a template objects.opEquals?
>
> auto opEquals(T, S)(T a, S b) { ... }
'case templates can't be virtual functions, so the following will not
work:
bool cmp (Object a, Object b) { return a.opQeuals(b); }
MyObj a, b;
cmp(a, b);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150713/b8847bc2/attachment.sig>
More information about the Digitalmars-d
mailing list