www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - general questions regarding value and reference semantics

reply "WhatMeWorry" <kc_heaser yahoo.com> writes:
I think I understand (and admire) D's use of value semantics for 
structs and reference semantics for classes. I believe classes 
exist for the entirety of a program's life whereas structs exist 
for the lifetime of their scope.

So I assume it is illegal for a struct to contain a class? And 
(holding my nose) pointers too?  In Andrei's Book he writes "a 
struct is just a glorified int".

However, it would be quite ok for a class to contain a struct?

Does a fixed-length, static array behave with value semantics 
while a dynamic array behave with reference semantics?


And then what to make of Phobos' std.container, when it says 
"DList uses neither reference nor value semantics."  Is there 
some hybrid or new undiscovered semantic I've never heard about?

Thanks for suffering this fool gladly.
Apr 28 2013
next sibling parent "evilrat" <evilrat666 gmail.com> writes:
On Monday, 29 April 2013 at 00:22:23 UTC, WhatMeWorry wrote:
 I think I understand (and admire) D's use of value semantics 
 for structs and reference semantics for classes. I believe 
 classes exist for the entirety of a program's life whereas 
 structs exist for the lifetime of their scope.
structs has scope lifetime. class objects lives as long as at least one reference points to them(their references(variables) has scoped lifetime) or until manually freed. class MyClass { this(int value) { _value = value; } int _value; property int val() { return _value; } } ... void doStuff() { MyClass mc = new MyClass(0); ... // mc will be released after function return } MyClass doWithReturn() { return new MyClass(1); } // if one need to change reference(not its object) one may use ref // this is also due to value variable semantics of references type void modify(ref MyClass my) { // old "my" still lives my = new MyClass(2); // assign new my to reference // old "my" now waiting to be collected } void notModified(MyClass my) { // current scope "my" is points to object with value = 1 my = new MyClass(3); // now it points to another object with value = 3 // object with value 3 will be collected after this func returns } void main() { MyClass my = new MyClass(5); // mc will exist only after entering this function until it's returns(but actual collection may proceed later) doStuff(); my = doWithReturn(); // now "my" has value = 1, after this point previous object with value 5 may be collected any time now. notModified(my); // though classes has value semantics "my" should be still points to MyClass with value = 1 modify(my); // now because this func accepts reference to myclass variables it actually modifes current scope "my" so now it's value = 2 } you can modify this simple example and see what's going on in debugger or with writeln.
 So I assume it is illegal for a struct to contain a class? And 
 (holding my nose) pointers too?  In Andrei's Book he writes "a 
 struct is just a glorified int".

 However, it would be quite ok for a class to contain a struct?
both class and struct may contain each other, but unlike structs class objects should be allocated and initialized before use.
 Does a fixed-length, static array behave with value semantics 
 while a dynamic array behave with reference semantics?
not sure about this one, all arrays(slices) works by value, if ony need to avoid copying one just pass it with ref to avoid copying. int[3] myarr; ... // by reference int sum(ref int[3] arr) { ... } // by value int sum(int[3] arr) { ... } ------------------- i hope this helps you, but don't take anything of this as absolute truth, i'm still D noob. also due to nature of your questions i assume you have no or little programming experience, so i suggest you read some books about both compiled and interpreted languages to get some understading about common principles.
Apr 28 2013
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/28/2013 05:22 PM, WhatMeWorry wrote:

 Does a fixed-length, static array behave with value semantics
Yes, a static array has value semantics. When assigned, a variable of a static array type will get its own elements, separate from the right hand side's elements. int[2] a; int[2] b = a; // b and a have separate elements int[2] c; b = c; // b's elements are copied from c's
 while a dynamic array behave with reference semantics?
Yes, because a dynamic array does not own any elements. It is a handle to reach those elements.
 And then what to make of Phobos' std.container, when it says "DList uses
 neither reference nor value semantics."  Is there some hybrid or new
 undiscovered semantic I've never heard about?
I don't have experience with DList but looking at its documentation, a DList is obviously a reference to a part of something called a chain. In that sense, DList has reference semantics. On the other hand, every DList variable owns the chain as well, because they can actually remove elemnents from it regardless of whether others are still referring parts of it. (Apparently, it is not undefined behavior nor it invalidates other DList variables: The other variables simply start seeing a shorter chain of elements.) Ali
Apr 28 2013