Const correctness revisited (proposal)

Walter Bright newshound1 at digitalmars.com
Mon Mar 24 01:59:48 PDT 2008


Oliver Dathe wrote:
> Hello D folks,
> 
> I'd like to introduce some suggestions for D2:
>   1.) Enforceable immutability of function calls regarding parameters
>   2.) Parameter related tail constness
>   3.) Enforceable tail constness on calls
> 
> In thread [1] the problem was stated, what a clean D2 version of a 
> function may look like, that returns a mutable slice from an input 
> string which (the input string) may not be mutable by the function 
> itself. There were no really clean solutions avoiding casts and 
> templates.

The reason there is no clean way to do this is because it is a 
fundamentally unsound operation to do it. Doing so violates the 
const-ness contract of the function. Please see my posts in the "const 
debacle" thread. In other words,

    T[] f(const(T)[] t)
    {
	return t;
    }

is wrong, wrong, wrong, and must not compile.



More information about the Digitalmars-d mailing list