Const sucks

Robert Fraser fraserofthenight at gmail.com
Mon Sep 10 19:37:41 PDT 2007


Walter Bright Wrote:

> Bruno Medeiros wrote:
> > Walter Bright wrote:
> >> What, exactly, is the use case that needs a solution?
> > 
> > Before hand, let me just say that by broken, I didn't mean subvertable, 
> > but rather that using const/invariant with classes would be impractical 
> > and unmanageable.
> > 
> > Simple example: Let's say you have a class Foo that is comparable. How 
> > do you sort an array of const(Foo)'s ?
> > 
> >   const(Foo)[] sort(const(Foo)[] arr) {
> >      ... ?
> > The sorting algorithm doesn't matter, what matters is: how would one 
> > swap two elements in the array?
> 
> You can't sort an (array of)(const T). You can sort an (array 
> of)(something of)(const T).

That worries me (not that case in particular, but general constness + classes). My coding style tends to use a lot of invariant objects (for thread safety reasons), so I get the feeling that the inability to *easily* express a mutable reference to a constant class will bite me like a ferret.



More information about the Digitalmars-d mailing list