How will we fix opEquals?

Jonathan M Davis jmdavisProg at gmx.com
Thu Feb 10 01:37:48 PST 2011


On Thursday 10 February 2011 01:26:14 so wrote:
> > Saying that no one should have to worry about const if they don't want
> > to is a
> > noble though, I suppose, but I don't think that it's entirely realistic.
> > const
> > is part of the language, and some things just plain have to be const to
> > work.
> > And given the prevalence of immutable with regards to threads and the
> > like,
> > you're going to be forced to use const in many cases anyway.
> > 
> > I don't think that it's all that big a deal with these 4 functions are
> > const.
> > The impact on the programmer is minimal. They can _almost_ ignore const
> > completely, since they can still ignore it pretty much everywhere else
> > other
> > than with the overriden const Object functions.
> > 
> > - Jonathan M Davis
> 
> Problem is not having to provide const for these functions, it is the
> lock-in we introduce by marking them const.
> These functions would be const most of the times, if not all. Still, it is
> hard to rule the other case out completely i suppose.

The thing is that opEquals, opCmp, toHash, and toString/writeTo _must_ be const 
for const and immutable objects to be able to use them. It's non-negotiable 
unless Object is breaking the type system (which would _not_ be good). That 
being the case, it doesn't matter how much we might want to avoid forcing const 
on people. We _must_ have it there, so anyone overriding those functions _must_ 
use it for those functions. They could create non-const versions in addition to 
the const ones, but they _must_ create the const versions. They're stuck. 
There's no way around it without breaking the type system. structs avoid the 
problem, because they don't have to worry about polymorphism, but classes 
obviously do.

Now, that forces people to use const on _4_ functions per class - that's it. And 
that's assuming that they want to override them all. They can ignore const 
beyond that. So, they _cannot_ ignore const completely. It's just not practical. 
However, they _can_ _mostly_ ignore it. So, it _does_ have an impact on those 
who don't wish to bother with const, but the impact should be minimal.

- Jonathan M Davis


More information about the Digitalmars-d mailing list