const debacle

Steven Schveighoffer schveiguy at yahoo.com
Fri Mar 21 10:50:44 PDT 2008


"Walter Bright" wrote
> Steven Schveighoffer wrote:
>> How do you declare a function that takes an array, is not allowed to 
>> change the array, but returns a slice into the argument array, and the 
>> return type matches the argument type.
>
>
> The way to do it is with a function template, parameterizing the type in 
> question.

So your answer essentially is, there is no way to specify that the function 
will not modify the parameter but returns the same type passed in?

Basically, I want a function like:

T[] foo(T)(in T[] source, in T[] pattern);

Where foo returns a slice of source that matches pattern, and promises not 
to modify source or pattern, but returns the same type as was passed in. 
Then the caller could modify the slice as necessary.  It's kind of like a 
'scoped' const, where the argument is only const within the function, but 
reverts to what it was passed in as once the function is exited.

What you're saying is, specify it like:

T[] foo(T)(T[] source, in T[] pattern);

Which means there is no check by the compiler to guarantee that source is 
not modified (unless T is const), and basically, the coder has to rely on 
the documentation to determine whether source will remain intact?  I think 
this degrades the benefits of const in this context.

-Steve 





More information about the Digitalmars-d mailing list