recursive equal, and firstDifference functions

Jonathan M Davis jmdavisProg at gmx.com
Tue Mar 19 14:10:59 PDT 2013


On Tuesday, March 19, 2013 21:50:55 Dan wrote:
> On Tuesday, 19 March 2013 at 20:28:09 UTC, Jonathan M Davis wrote:
> > The language and standard library are designed around them being
> > part of the types themselves. Stuff like AAs won't work if
> > those functions
> > aren't defined on the types themselves.
> 
> Sorry, I don't understand the last statement.

Lots of stuff uses ==, toHash, etc. That's the way the language is designed. 
Defining your types without defining those properly just isn't going to work for 
a _lot_ of stuff. Creating an external function to compare objects or generate 
hashes or anything like that is just going to cause problems, because only 
your stuff would use it. The built-in stuff wouldn't, and the standard library 
wouldn't. For instance, AAs require that opEquals and toHash be defined, and no 
matter how clever your external functions for comparing objects or generating 
hashes are, they're not going to work with the built-in AAs. Any type which is 
going to work with the built-in AAs must define opEquals and toHash.

So, if the problem is boilerplate code, mixins can be used for opEquals, 
opCmp, etc. in order to define those for you, but creating external functions 
to do those same operations is not how the language was designed and isn't how 
anything is going to expect types to work. Anything that wants to be 
comparable, should define ==. Anything that wants to be hashable should define 
toHash. Etc. Something like equalRecurse ultimately really makes no sense at 
all.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list