www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Strict definition of value types and reference types

reply "Nicholas Smith" <nmsmith65 gmail.com> writes:
Two things that I can't seem to nail down a strict definition for 
are value types and reference types. I know very well their basic 
definitions: a variable of a value type holds its own data and 
assignment copies the data, and a variable of a reference type 
holds a pointer to its data and assignment (to the same type or a 
superclass at least) copies the pointer.

Unfortunately it's easy to find a case where those definitions 
don't fit. For example, it is said that slices are reference 
types. Sure, slices hold references to an array, but they also 
carry their own data, which gives them value semantics. For 
example, below I introduce my new integer type which I think we 
can all agree should be part of the D language.

import std.stdio;
alias Integer = void[];

void main()
{
	Integer num1, num2;

	write("Enter two non-negative numbers to add separated by a 
space: ");
	size_t v1, v2;
	readf(" %s %s\n", &v1, &v2);
	num1.assign(v1);
	num2.assign(v2);
	Integer result = num1.add(num2);

	writeln("Answer: ", result.toSize_t());
}

Integer assign(ref Integer lvalue, size_t rvalue)
{
	lvalue.length = rvalue;
	return lvalue;
}

Integer add(Integer num1, Integer num2)
{
	Integer result;
	result.assign(num1.toSize_t() + num2.toSize_t());
	return result;
}

size_t toSize_t(Integer num)
{
	return num.length;
}

In essence, are not all types just structs, with different 
operator overloads to give different semantics? If so, how do we 
classify 'value type' versus 'reference type'? What if I have a 
struct whose members are a fixed-length array and a slice, and 
different operations act on different members? The only thing I 
can really consider to be a reference type is a plain reference 
to an instance of a class.
Apr 13 2013
next sibling parent "Nicholas Smith" <nmsmith65 gmail.com> writes:
Re-wording my last paragraph because of incorrect wording:

In essence, don't all variables act as structs, with different 
operator overloads to give different semantics? If so, how do we 
classify 'value type' versus 'reference type'? What if I have a 
struct whose members are a fixed-length array and a slice, and 
different operations act on different members? The only thing I 
can really consider to be a variable of a reference type is a 
plain reference
to an instance of a class.
Apr 13 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Nicholas Smith:

 it is said that slices are reference types. Sure, slices hold 
 references to an array, but they also carry their own data, 
 which gives them value semantics.
Slices are a small value, that contains in place a length and a pointer. The pointer refers to memory allocated elsewhere. So slices are kind of hybrids between values and references. You can also think of them as fat references. When you program in D you must remember this nature of sliced, otherwise your code will not work... Bye, bearophile
Apr 13 2013
parent reply "Nicholas Smith" <nmsmith65 gmail.com> writes:
On Sunday, 14 April 2013 at 02:51:35 UTC, bearophile wrote:
 slices are kind of hybrids between values and references. You 
 can also think of them as fat references. When you program in D 
 you must remember this nature of sliced, otherwise your code 
 will not work...

 Bye,
 bearophile
Ali Çehreli uses slices as the first example for what a reference type is in his "Programming in D". Perhaps that is not a good example then.
Apr 13 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/13/2013 08:46 PM, Nicholas Smith wrote:

 On Sunday, 14 April 2013 at 02:51:35 UTC, bearophile wrote:
 slices are kind of hybrids between values and references. You can also
 think of them as fat references. When you program in D you must
 remember this nature of sliced, otherwise your code will not work...

 Bye,
 bearophile
Ali Çehreli uses slices as the first example for what a reference type is in his "Programming in D". Perhaps that is not a good example then.
That must be fixed then. :/ Slices are value types but they have reference semantics: they are references to elements but a slice variable itself is data on its own. Your definition of value type vs. reference type is correct. These definitions come from how the language behaves. structs are value types and classes are reference types. structs are the most powerful because they can be used to provide value semantics or reference semantics. Ali
Apr 13 2013