__traits() to get parameter details only ? ... hasMember looks up everything within

Ali Çehreli acehreli at yahoo.com
Thu Aug 5 15:23:24 UTC 2021


On 8/4/21 7:21 PM, someone wrote:

 > somewhere I read
 > Ali saying there's nothing wrong implementing properties the old-way via
 > functions because @property has nothing special about it but I can't
 > state where I read what I am stating so take it with a grain of salt.

As I understand it, @property is discouraged by the community. That's 
what I say here:

   http://ddili.org/ders/d.en/property.html#ix_property.@property

Quoting the whole section:

<quote>
@property

Property functions may be defined with the @property attribute as well. 
However, as a best practice, the use of this attribute is discouraged.

import std.stdio;

struct Foo {
     @property int a() const {
         return 42;
     }

     int b() const {    // ← Defined without @property
         return 42;
     }
}

void main() {
     auto f = Foo();

     writeln(typeof(f.a).stringof);
     writeln(typeof(f.b).stringof);
}

The only effect of the @property attribute is when determining the type 
of an expression that could syntactically be a property function call. 
As seen in the output below, the types of the expressions f.a and f.b 
are different:

int            ← The type of the expression f.a (the return type)
const int()    ← The type of the member function Foo.b
</quote>

Ali

P.S. I found that section by searching for "property" on the Index page:

   http://ddili.org/ders/d.en/ix.html

The Index page is linked from the title page (aka index.html) as "The 
Index Section". (Too many indexes! :) )

   http://ddili.org/ders/d.en/index.html




More information about the Digitalmars-d-learn mailing list