@property Incorrectly Implemented?

Chris Wright via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 16 15:41:58 PDT 2016


On Fri, 16 Sep 2016 20:52:37 +0000, John wrote:
> You also made a reference to C#, which doesn't even
> allow you take address of So I don't know what you mean by that's how I
> want to play, when you instigated said argument.

C# allows you to use pointers and take the address of fields. You must 
compile your code with `/unsafe` and use an `unsafe {}` block to do it.

It does not allow you to take the address of a property. It complains: 
"Cannot take the address of the given expression".

> as far as I am aware there is no way to get the delegate for the setter.

Let's consider the following type:

  struct Foo {
    @property int a() { return 1; }
    @property int a(int v) { return v; }
  }

Given an instance `Foo f`, `f.a` is an overload set. It's a bundle of 
several functions that can be referred to using the same symbols and must 
be disambiguated.

Most of the time, you disambiguate by calling the function with 
arguments. However, that is not the only way: when taking the address of 
a function, the compiler looks at the static type that the expression is 
expected to return.

So the following works:

  int delegate(int) setter = &f.a;
  int delegate() getter = &f.a;

But this doesn't:

  auto dg = &f.a;
  // Error: cannot infer type from overloaded function symbol &f.a

The same principle applies for any overloaded functions.

> The way you should check if a member is a property shouldn't be by
> taking the address of it.

Check if __traits(getOverloads) for the member name has at least one 
overload. Or __traits(getVirtualFunctions) and see if there's a matching 
name.

Unfortunately, std.traits doesn't have anything already implemented to do 
this.

You may want to test things out and check the specific compiler error 
messages before declaring that they don't work. They can be rather dense, 
though.


More information about the Digitalmars-d mailing list