D - properties with two arguments

D language link states that

@Property functions can only have zero, one, or two parameters.

It is clear enough which properties have zero and one parameter, and there is an example for each in the link. However, the link says nothing about two-parameter properties, except that they are allowed. So what are they actually doing? What do the parameters for this property mean?

+4
source share
1 answer

A property with two parameters will be the UFCS installer. Suppose we had:

struct Whatever {
   void setProperty(int);
}

And you wanted to add the setter property instead of calling setProperty, but you cannot change the structure for any reason. You can add an external function as follows:

@property int my_prop(ref Whatever _this, int prop_value) {
       _this.setProperty(prop_value);
       return prop_value;
}

:

Whatever w;
w.my_prop = 10;

, this, - .

+6

Source: https://habr.com/ru/post/1680638/


All Articles