ref arguments

Steven Schveighoffer schveiguy at yahoo.com
Mon Sep 21 08:43:28 PDT 2009


On Mon, 21 Sep 2009 11:12:49 -0400, #ponce <aliloko at gmail.com> wrote:

> Is there a reason to use ref instead of in, inout or out ?
> I'm using D1 and it's not in the spec.

ref and inout are synonymous, ref is preferred, as inout is essentially a  
defunct keyword.

in means that it's a reference that cannot be changed.  In D1, it means  
you cannot change the value, but if the value contains a reference to  
something else (like an array), you can change what it points to.  In D2,  
it is synonymous with ref const scope (although the scope attribute  
doesn't yet mean anything), so you cannot change anything it points to.

out means that it's a reference that you are always going to overwrite.  I  
believe the compiler even initializes the value for you upon function  
entry.

So in summary:

inout: don't use it.
ref: Use this for pass by reference for a value that you may or may not  
change.
in: Use this for pass by reference for a value that you won't change
out: Use this for pass by reference for a value that you will *always*  
change.

-Steve


More information about the Digitalmars-d-learn mailing list