Const by Default

Jason House jason.james.house at gmail.com
Sun Jun 24 19:41:36 PDT 2007


David B. Held wrote:
> I think an interesting point was brought up in the earlier CbD thread, 
> which is specifically the issue of c'tors, and generally, the issue of 
> passing mutable references.  So consider:
> 
> class MyObj
> {
>     this(MyClass a, MyClass b)
>     {
>         a_ = a;
>         b_ = b;
>     }
> private:
>     MyClass a_;
>     MyClass b_;
> }
> 
> With CbD, this code is incorrect.  Would you like to explain to a novice 
> programmer why?  Now, let's try to fix it:
> 
>     this(ref MyClass a, ref MyClass b)
>     {
>         a_ = a;
>         b_ = b;
>     }
> 
> This code works, but now you have to explain to the student that even 
> though classes are always passed by reference in D, you have to actually 
> spell it out explicitly sometimes.  And further note that the only 
> reason this form is not less efficient is because references to 
> references are collapsed to simple references.  Otherwise, there would 
> be an extra level of indirection here.


What happened to inout?  To me, using inout would make perfect sense to 
a student.  Is ref just an alias for inout or is there something deeper 
that I'm missing?



More information about the Digitalmars-d mailing list