Passing arguments into functions - in, out, inout, const, and contracts

Bill Baxter dnewsgroup at billbaxter.com
Sat Feb 10 22:22:01 PST 2007


Jason House wrote:
> I believe that everything (except maybe fundamental types such as int) 
> are passed by reference.  I'd expect "in" parameters to be considered 
> const, but I know that member functions can't be declared const.
> 
> It seems to me that in/out/inout has no effect on how the language 
> operates.  I'm wondering how those affect the language.  Does "in" 
> impose a contract that the ending value must equal the starting value? I 
> also haven't thought of any good way to differentiate out from inout 
> from the compiler's standpoint (obviously, it makes sense to users).

All structs are passed by value unless you use 'inout'.
Also you can't change the identity of an object reference unless you 
pass it inout.  As in:

void make_me_new_again(inout Object obj)
{
    obj = new Object;
}
. . .
make_me_new_again(myObj);


If that wasn't inout the caller would still be holding onto the same 
object that was passed in.

inout makes plenty of difference.

Don't know about out.  It's pretty much the same as inout, with 
(perhaps?) some compile-time checks to make sure you don't try to read 
the value in the function.  Seems pretty useless to me.  Much more 
useful would be inbyref or some kind of const reference parameter.  But 
that's supposedly in the works.

'In' is just the default.  If you don't specify anything it's 'in'.  But 
it doesn't enforce or check any pre or post conditions.

--bb


More information about the Digitalmars-d-learn mailing list