digitalmars.D.learn - fields in property ?
- MIURA Masahiro (23/23) Jan 12 2010 Hi,
- Jesse Phillips (13/36) Jan 13 2010 @property is fairly new so documentation is lacking and no one really
- Simen kjaeraas (33/56) Jan 13 2010 @property {
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
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
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









Jesse Phillips <jessekphillips+D gmail.com> 