fields in @property ?
Jesse Phillips
jessekphillips+D at gmail.com
Wed Jan 13 18:12:51 PST 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; }
}
More information about the Digitalmars-d-learn
mailing list