On 08/06/2016 03:38 AM, Chris Wright wrote:
Some reflection stuff is a bit inconvenient:

class A {
  int foo() { return 1; }
}

void main() {
  auto a = new immutable(A);
  // This passes:
  static assert(is(typeof(a.foo)));
  // This doesn't:
  static assert(__traits(compiles, () { a.foo; }));
}

__traits(compiles) is mostly an evil hack, but things like this require
its use.

The two are not equivalent, though. The first one checks the type of the method.

__traits(compiles, ...) also passes when used that way:

    static assert(__traits(compiles, a.foo)); /* passes */

Add parentheses to the typeof one and it fails as expected:

    static assert(is(typeof(a.foo()))); /* fails */

Can also do the function literal thing you did in the __traits one:

    static assert(is(typeof(() { a.foo; }))); /* fails */

If the method wasn't there at all, they would all fail, of course. So the programmer couldn't mess this up as easily. I don't think this outweighs the inconvenience of bending over backwards to avoid immutable, though.

Reply via email to