ref int value() { return m_value; } - uses?

Joel Christensen joelcnz at gmail.com
Fri Jul 29 15:57:39 PDT 2011


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 );
	}
}


More information about the Digitalmars-d-learn mailing list