Why use @property in D?

I realized by trying that

struct PropertyTest { @property int x() { return val; } @property void x( int newVal ) { val = newVal; } void test() { int j; j = x; x = 5; } private: int val; } 

does the same when I leave @property . Everything compiles fine. What is the point then for declaring functions like @property ?

By the way, I am using the dmd2 compiler.

+7
source share
2 answers

The reason they work without @property is because @property was added after they enabled the property method syntax. Adding -property to your DMD command line involves using the @property annotation. This is not the default for backward compatibility reasons. Someday it will become standard (or, as they say), so it is best to compile with -property to make sure you are commenting correctly.

+9
source

It allows you to use the no-arg method without parentheses (for example, reading a variable) and allows you to call a method with one argument without parentheses, as you assign a variable.

 @property int foo() { ... } @property void bar(int x) { ... } void main() { bar = foo; } 

You must specify -property as the command line parameter for the compiler.

+1
source

All Articles