general questions regarding value and reference semantics

evilrat evilrat666 at gmail.com
Sun Apr 28 20:34:11 PDT 2013


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.


More information about the Digitalmars-d-learn mailing list