Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use @property in D?

Tags:

properties

d

I figured out 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 exactly the same when I leave the @property out. Everything compiles fine. What's the point then for declaring functions as @property?

BTW, I'm using the dmd2 compiler.

like image 925
Ralph Tandetzky Avatar asked Aug 23 '12 15:08

Ralph Tandetzky


People also ask

What is the purpose of @property Python?

Python's property() is the Pythonic way to avoid formal getter and setter methods in your code. This function allows you to turn class attributes into properties or managed attributes. Since property() is a built-in function, you can use it without importing anything.

What does %D stand for in Java?

The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character.

When should you use properties?

Consider using a property if the member represents a logical attribute of the type. Do use a property, rather than a method, if the value of the property is stored in the process memory and the property would just provide access to the value.

Why we are using properties?

Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code. A get property accessor is used to return the property value, and a set property accessor is used to assign a new value.


1 Answers

The reason they work without @property is because @property was added after they allowed the property method syntax. Adding -property to your DMD command line enforces use of @property annotation. It's not the default for backward compatibility reasons. Someday it will become the default (or so they say) so it's best to compile with -property to ensure you are annotating properly.

like image 100
eco Avatar answered Sep 20 '22 11:09

eco