D - more or less power than C++?

Regan Heath regan at netwin.co.nz
Sun Mar 5 00:18:03 PST 2006


On Sat, 04 Mar 2006 23:18:00 -0500, Tyro <ridimz_at at yahoo.dot.com> wrote:
> Oh, I see, you want the in parameter to treat it as if it were read  
> only. Rightly so. One should not be able to modify an "in" parameter  
> (reference or otherwise). It is the implied behavior when using the in  
> keyword, there it is only reasonable to expected it. I do not think this  
> is a problem with the implementation of const however, but rather a  
> glaring defect in the implementation of "in".

One cannot modify an "in" parameter. All "in" parameters are copied in, in  
other words the parameter inside the function is always a copy of the  
variable passed. The 'key' issue here is realising what you're passing.  
When you pass a 'reference' the variable is the 'reference' itself, _not_  
the data to which it refers. Therefore the 'reference' is what is copied  
in and what is protected by "in". "in" makes no guarantee to protect the  
data to which that reference refers.

I think we can all agree that having some way to declare a function is not  
going to perform any sort of write operation using a passed reference and  
having the compiler protect this accidentally happening would be a good  
thing. The problem is how to achieve that, see the many previous "const"  
and "readonly" threads for a detailed discussion of why it's not so simple  
to solve.

Regan



More information about the Digitalmars-d mailing list