[Issue 4290] 'Fragile' opCmp/toHash signature errors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Sep 20 16:47:03 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4290



--- Comment #7 from bearophile_hugs at eml.cc 2010-09-20 16:46:25 PDT ---
It seems we are in irreducible disagreement here.

> But opCmp is a valid function!  I can still call it, even though it's not put
into the typeinfo.

Of course. But when it is well formed it is also a *special* function, because
the compiler uses it automatically in some useful situations.


> But it's not a bug!

If you define a Foo struct like this, it' quite probably a bug in your code:

struct Foo {
    int x;
    const bool opEquals(ref const Foo f) {
        return true;
    }
    int opCmp(ref const Foo f) {
        return 0;
    }
    hash_t toHash() {
        return 10;
    }
}


> There is nothing inherently special about opCmp.

It's special because there is a contract between the compiler and the
programmer, that such functions are used for example when you sort an array of
structs or you put them in an associative array. If the compiler in some
situations silently doesn't use such functions when the programmer surely
intended them to be used that way by the compiler, then there's something wrong
with the compiler.


> We need to add special notation conveying our intentions to the compiler

This is wrong. D language is designed to adopt the safe behaviour (= less bug
prone, I am not talking about safeD) on default. So if feel the need of it,
then you may add special notation to say the compiler to not use a function
named opCmp for comparisons.


> It's like refusing to compile a function because it's not an english word.

This is not the same thing. In Foo there are functions that have exactly the
same name as the standard methods, and their signature too is very similar. I
am not asking for the compiler to give a warning for a opcompare or even opcmp
function.

And by the way, what I am asking is an extension of what dmd is already doing,
because in many other cases if you try to compile a struct that defined a wrong
opCmp or toHash the compiler do shows an error. So I am asking for the error
messages to cover this case too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list