Using ()s in @property functions

Steven Schveighoffer schveiguy at yahoo.com
Tue Jun 29 04:30:22 PDT 2010


On Tue, 29 Jun 2010 01:53:05 -0400, Nick Sabalausky <a at a.a> wrote:

> "dsimcha" <dsimcha at yahoo.com> wrote in message
> news:i0bme6$2phb$1 at digitalmars.com...
>> Once enforcement of @property is enabled, we need to decide whether
>> calling an
>> @property function using ()s should be legal.  In other words, should
>> @property **require** omission of ()s or just allow it?  My vote is for
>> just
>> allowing omission, because I've run into the following ambiguity while
>> debugging std.range.  Here's a reduced test case:
>>
>> struct Foo {
>>    uint num;
>>
>>    @property ref uint front() {
>>        return num;
>>    }
>> }
>>
>> void main() {
>>    Foo foo;
>>    uint* bar = &foo.front;  // Tries to return a delegate.
>> }
>>
>> If I can assume that @property functions can be called with explicit ()s
>> to
>> forcibly disambiguate this situation, then I can fix these kinds of bugs
>> by
>> simply doing a:
>>
>> uint* bar = &(foo.front());
>>
>> Can we finalize the idea that this will continue to be allowed now so  
>> that
>> I
>> can fix the relevant bugs in Phobos and know that my fix won't be broken
>> in a
>> few compiler releases?
>
> Crazy idea:
>
> The whole point of properties is to simulate a member that's *not* a
> function. With that in mind, does it even make sense to allow the use of
> unary "&" to get a delegate to a property at all? On the off-chance that  
> you
> really do need a delegate to a setter/getter you can just make a lambda -
> and that works exactly the same even if it's a real member variable  
> instead
> of a property. And inlining could take care of any performance issues.

I don't think a delegate to a property is a common need.  Besides, you  
would almost expect something that accesses both read/write properties.   
I'm sure someone can come up with a way to do this in a type-agnostic way  
(a "property delegate" if you will...)

If one truly needs a delegate to a property, why not use __traits/meta to  
do it?

meta.delegateOf(foo.front);

This could actually solve the issue of how to get a specific delegate:

struct S
{
     void foo(int x);
     void foo(string s);
}

S s;
auto d1 = meta.delegateOf(s.foo, int);
auto d2 = meta.delegateOf(s.foo, string);

-Steve


More information about the Digitalmars-d mailing list