const?? When and why? This is ugly!

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Mar 6 16:21:08 PST 2009


Burton Radons wrote:
> Andrei Alexandrescu Wrote:
> 
>> This is a known problem for hich we will provide a solution.
> 
> If you have something which works everywhere please tell us because
> we've been trying to find one for a long time, but as far as I know
> there is no solution. The best I've ever gotten to is:
> 
> // "A" means that the constness of the return type depends upon the
> constness of the argument. There are dozens of ways to specify the
> same thing. const (A) mstring match (const (A) mstring text, RE
> expression);
> 
> But setting aside whether that helps or hinders self-documentation,
> that's far from the only place at which you put mutable data through
> a const section that you need to modify later. What if the function
> were instead:
> 
> struct REMatch { string match; /// The matched string. size_t offset;
> /// Offset within the string where the match occurs. string []
> groups; /// Matched groups.
> 
> this (string text); }
> 
> What am I going to do about this now without using templates? If you
> define a special syntax to make this work, then I can give you
> something even further which won't.

The problem is you set up artificially constrained rules, i.e. "without 
using templates". You can't use the same struct to store mutable types 
and non-mutable types mixed with always-mutable types, and for good 
reasons. No type system will allow 100% of the correct programs to run. 
Why the fuss. Use a gorram template and call it a day.

Andrei



More information about the Digitalmars-d mailing list