Thread overview | |||||
---|---|---|---|---|---|
|
January 13, 2010 fields in @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 ---- |
January 14, 2010 Re: fields in @property ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MIURA Masahiro | 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; }
}
|
January 14, 2010 Re: fields in @property ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MIURA Masahiro | 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 |
Copyright © 1999-2021 by the D Language Foundation