Value vs. reference semantics, and pointers

Bruno Medeiros daiphoenixNO at SPAMlycos.com
Thu Mar 23 12:41:42 PST 2006


Oskar Linde wrote:
> Hi,
> 
> I will just give you a few quick comments.
> 
> Scott L. Burson wrote:
>>  Similarly, I would discourage the use of the
>> ampersand to reify a function; I think the ampersand should at least 
>> be optional
>> (as indeed it is in C) -- I don't see anything in the docs that says it's
>> required, but you seem to use it in all the examples. 
> 
> I guess the reason is that D allows function calling (property like) 
> without trailing parentheses (). Meaning func is identical to func() in 
> most cases. The ampersand is needed to distinguish function calls from 
> function references.
> 
>> I've been skimming the material on the D Web site and want to be sure I
>> understand some things.  I gather that structs and unions have value 
>> semantics,
>> while arrays and classes have reference semantics.  That is, 
>> assignment to a
>> variable of struct or union type copies the contents, while assignment 
>> to a
>> variable of array or class type copies a reference to the contents.  
>> Is that
>> correct?  You might want to clarify this in the docs, as it's pretty
>> fundamental.
> 
> That is correct.
> 
>> I also have a question about the treatment of pointers.  I understand 
>> that
>> out/inout parameters and reference semantics for classes and arrays will
>> eliminate the vast majority of occasions calling for explicit 
>> pointers, but
>> still, I'm curious.  In D, can you make a pointer to a single object 
>> as in C, or
>> does it have to be in an array?
> 
> You can still make pointers to anything just like in C.
> 
> /Oskar
That is not correct (and you know it, have you forgotten?).
A static array (and by static array we mean an array of fixed size, as 
C's arrays) is neither a proper value or reference type. It is an odd 
mix of the two, and IMO a bad discrepancy. Perhaps this is something D 
could be improved upon. (don't a formed ideia how, though)
Dynamic arrays (dynamic length arrays) are "a bit more" than a reference 
type, but they behave pretty much as reference type, so one can consider 
them as such.

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list