www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ref int value() { return m_value; } - uses?

reply Joel Christensen <joelcnz gmail.com> writes:
What use is the property thing?

class Test {
	private int m_value;
	 property ref int value() { return m_value; }
}

void main() {
	auto test = new Test;
	test.value += 1;
}

Or this:
import std.stdio: writeln;
import std.conv: to;

class Test {
	private string m_text;
	
	ref auto text( string tx ) {
		if ( tx.length > 4 )
			m_text = m_text[ 0 .. 4 ];
		return m_text;
	}
	
	override string toString() {
		return text( m_text );
	}
}

void main() {
	auto test = new Test;
	with( test ) {
		text( to!string( test ) ) = "feet";
		writeln( test );
		text( to!string( test ) ) ~= " ";
		writeln( test );
	}
}
Jul 29 2011
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday 30 July 2011 10:57:39 Joel Christensen wrote:
 What use is the property thing?
 
 class Test {
 	private int m_value;
 	 property ref int value() { return m_value; }
 }
 
 void main() {
 	auto test = new Test;
 	test.value += 1;
 }
 
 Or this:
 import std.stdio: writeln;
 import std.conv: to;
 
 class Test {
 	private string m_text;
 
 	ref auto text( string tx ) {
 		if ( tx.length > 4 )
 			m_text = m_text[ 0 .. 4 ];
 		return m_text;
 	}
 
 	override string toString() {
 		return text( m_text );
 	}
 }
 
 void main() {
 	auto test = new Test;
 	with( test ) {
 		text( to!string( test ) ) = "feet";
 		writeln( test );
 		text( to!string( test ) ) ~= " ";
 		writeln( test );
 	}
 }
http://www.d-programming-language.org/property.html property makes it so that the function is used syntactically as a variable rather than a function. And while it's not currently enforced, eventually, you will _have_ to use property functions with the variable syntax. For instance, empty on ranges is marked with property: assert(range.empty); You could currently do assert(empty(range)); but eventually that will be illegal. Property functions allow you to have public member variables become functions (or functions become public member variables) without having to change the code which uses them. They're generally used instead of getters and setters. In this particular case, the property function returns a ref, so it's both a getter and a setter. - Jonathan M Davis
Jul 29 2011
parent reply Joel Christensen <joelcnz gmail.com> writes:
 http://www.d-programming-language.org/property.html

  property makes it so that the function is used syntactically as a variable
 rather than a function. And while it's not currently enforced, eventually, you
 will _have_ to use property functions with the variable syntax. For instance,
 empty on ranges is marked with  property:

 assert(range.empty);

 You could currently do

 assert(empty(range));

 but eventually that will be illegal. Property functions allow you to have
 public member variables become functions (or functions become public member
 variables) without having to change the code which uses them. They're
 generally used instead of getters and setters.

 In this particular case, the property function returns a ref, so it's both a
 getter and a setter.

 - Jonathan M Davis
Thanks for the reply Davis. With the getter/setter method you don't have much control, like knowing what value it being set to at the method definition (could with 'ref auto xpos( int value ) { ... return m_xpos; }' :-/). And having two methods, one for getter and one for setter, you can't do stuff like 'xpos++;' - Joel Christensen
Jul 31 2011
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday 01 August 2011 12:00:02 Joel Christensen wrote:
 http://www.d-programming-language.org/property.html
 
  property makes it so that the function is used syntactically as a
 variable rather than a function. And while it's not currently enforced,
 eventually, you will _have_ to use property functions with the variable
 syntax. For instance, empty on ranges is marked with  property:
 
 assert(range.empty);
 
 You could currently do
 
 assert(empty(range));
 
 but eventually that will be illegal. Property functions allow you to
 have
 public member variables become functions (or functions become public
 member variables) without having to change the code which uses them.
 They're generally used instead of getters and setters.
 
 In this particular case, the property function returns a ref, so it's
 both a getter and a setter.
 
 - Jonathan M Davis
Thanks for the reply Davis. With the getter/setter method you don't have much control, like knowing what value it being set to at the method definition (could with 'ref auto xpos( int value ) { ... return m_xpos; }' :-/). And having two methods, one for getter and one for setter, you can't do stuff like 'xpos++;'
The situation could definitely use some improvement, and there's still more ironing out that needs be done with property implementation-wise. property is supposed to _disallow_ the use parens and the lack of property is supposed to _require_ the use of parens, but we can't enforce that yet due to issues with the implementation, and the standard library needs to be adjusted for it as well. I believe that the -property flag (or something similar) was added to dmd with 2.054 to enable the enforcement of property, so we can experiment with that enforcement now, but we can't always enable it yet. So, things like not being able to use the increment and decrement operators on properties that aren't ref could very well be fixed - I would certainly hope so. But it _is_ a bit of an annoyance at the moment. - Jonathan M Davis
Jul 31 2011