www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - fields in property ?

reply MIURA Masahiro <echochamber gmail.com> writes:
Hi,

Is it ok to have fields in  property ?

Implementing properties often require private fields to hold the
values, and it seems natural to have them inside  property near
the property methods.  But I couldn't find any mention in the docs
about fields in  property.

This compiles with DMD 2.039, and runs fine:

---- cut here ----
import std.stdio;

class Foo
{
    this(string msg) { this._msg = msg; }

     property {
	private string _msg; // <-- field in  property
	string msg() { return _msg; }
    }
}

void main()
{
    auto foo = new Foo("Hello world");
    writeln(foo.msg);
}
---- cut here ----
Jan 12 2010
next sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
MIURA Masahiro wrote:

 Hi,

 Is it ok to have fields in  property ?

 Implementing properties often require private fields to hold the
 values, and it seems natural to have them inside  property near
 the property methods.  But I couldn't find any mention in the docs
 about fields in  property.

 This compiles with DMD 2.039, and runs fine:

 ---- cut here ----
 import std.stdio;

 class Foo
 {
     this(string msg) { this._msg = msg; }

      property {
 	private string _msg; // <-- field in  property
 	string msg() { return _msg; }
     }
 }

 void main()
 {
     auto foo = new Foo("Hello world");
     writeln(foo.msg);
 }
 ---- cut here ----
property is fairly new so documentation is lacking and no one really knows exactly what is allowed. But if you are familiar with the purpose of properties and what property is replacing (functions not requiring parentheses), your code makes since to be valid. Currently D2 still allows calling of functions without parentheses, this I believe is supposed to change. You can also have properties that are overloaded. property { void foo(string num)... void foo(int num)... int foo() { return _num; } }
Jan 13 2010
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
On Wed, 13 Jan 2010 08:30:39 +0100, MIURA Masahiro <echochamber gmail.com>  
wrote:

 Hi,

 Is it ok to have fields in  property ?

 Implementing properties often require private fields to hold the
 values, and it seems natural to have them inside  property near
 the property methods.  But I couldn't find any mention in the docs
 about fields in  property.

 This compiles with DMD 2.039, and runs fine:

 ---- cut here ----
 import std.stdio;

 class Foo
 {
     this(string msg) { this._msg = msg; }

      property {
 	private string _msg; // <-- field in  property
 	string msg() { return _msg; }
     }
 }

 void main()
 {
     auto foo = new Foo("Hello world");
     writeln(foo.msg);
 }
 ---- cut here ----
property { //stuff } means that anything within the curly braces has the property attribute. For fields, property does nothing, so class foo { property { int bar; int baz( ) { return bar; }; } } is equivalent to class foo { int bar; property { int baz( ) { return bar; } } } In other words, this is not (or at least should not be) possible: class foo { property { int bar; int baz( ) { return bar; }; } property { int bar; // Duplicate foo.bar. int qux( ) { return bar; } // Will not return the latter bar. } } -- Simen
Jan 13 2010