references and function calls

Neal Becker ndbecker2 at gmail.com
Fri Mar 16 05:01:19 PDT 2007


Frits van Bommel wrote:

> Neal Becker wrote:
>> I've been reading the D specification.  A couple of questions:
>> 1. Do I understand correctly that D has no reference type declarations
>> (there is no e.b., int& x;)?
> 
> Yes. (but see below for parameters)
> 
>> 2. Reading the section on functions, unfortunately I don't think it is
>> clear
>> on how arguments are passed.  Do I understand correctly that argument
>> passing is like java: POD are passed by value but other objects (class
>> instances) are passed by reference?
> 
> By default:
> Everything but objects and arrays are passed by value.
> Objects are passed by reference (or perhaps more correctly: the
> reference to the data is passed by value).
> Arrays are similar: The data in the array is passed by reference, but
> the reference to it is passed by value. In the case of dynamic arrays,
> the reference also contains the length so this makes a difference.
> 
> But this behavior can be changed by specifying 'inout' or 'out'.
> Both of those tell the compiler the parameter is to be passed by
> reference. In the case of objects and arrays, the reference is passed by
> reference. That means you can change the object or data being referred to
> and/or the length of dynamic arrays.
> The difference between 'out' and 'inout' is that 'out'
> default-initializes the parameter at the start of the function, so you
> can't pass in data that way.
> 
> You can also specify 'in', but that specifies the default behavior.
> 
> There's also 'lazy', which turns the expression passed into a delegate
> that evaluates the expression. The expression is only evaluated if the
> delegate is ever called, but it's re-evaluated every time it's called.

I've found one of the biggest sources of complication of template
programming in c++ was the variety of parameters:
T, T& T const&

I'm wondering if D lack of reference types will simplify things here, or do
the same issues just show up in a different guise?


More information about the Digitalmars-d-learn mailing list