@property (again)

luka8088 luka8088 at owave.net
Wed Nov 20 23:53:34 PST 2013


On 21.11.2013. 4:14, Manu wrote:
> It would be nice to have a commitment on @property.
> Currently, () is optional on all functions, and @property means nothing.
> I personally think () should not be optional, and @property should
> require that () is not present (ie, @property has meaning).
> 
> This is annoying:
>   alias F = function();
> 
>   @property F myProperty() { return f; }
> 
>   Then we have this confusing situation:
>     myProperty(); // am I calling the property, or am I calling the
> function the property returns?
> 
> This comes up all the time, and it really grates my nerves.
> Suggest; remove @property, or make it do what it's supposed to do.

Fix it!

struct S {
  auto f1 () { return 1; }
  auto f2 () { return { return 2; }; }

  @property auto p1 () { return 5; }
  @property auto p2 () { return { return 6; }; }
}

unittest {

  S s1;

  // don't breat current function call behavior
  // don't deal with optional () now
  // (but also don't break their behavior)
  assert(s1.f1 == 1);
  assert(s1.f1() == 1);

  assert(s1.f2()() == 2);
  auto fv = s1.f2;
  assert(fv() == 2);

  // make sure @propery works as described
  // in http://dlang.org/property.html#classproperties
  assert(s1.p1 == 5);
  static assert(!__traits(compiles, s1.p1() == 5));

  assert(s1.p2() == 6);
  static assert(!__traits(compiles, s1.p2()() == 6));

  auto pv1 = s1.p1;
  assert(pv1 == 5);

  auto pv2 = s1.p2();
  assert(pv2 == 6);

}

This test is according to the documentation and as far as I remember
everyone agrees that this is how @property should behave.


More information about the Digitalmars-d mailing list