Using ()s in @property functions

Nick Sabalausky a at a.a
Tue Jun 29 00:28:44 PDT 2010


"Robert Jacques" <sandford at jhu.edu> wrote in message 
news:op.ve1us1xj26stm6 at sandford.myhome.westell.com...
> On Tue, 29 Jun 2010 01:53:05 -0400, Nick Sabalausky <a at a.a> wrote:
>
>>
>> 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.
>
> 1. You'd get a closure, not a lambda, which generally would require a heap 
> allocation.
> 2. The idea's not too crazy, I've been having similar thoughts. However,
> 3. D's a system's programming language and not being able to get a 
> function pointer to a function seems wrong.
> 4. There's actually valid use cases where getting the address of a 
> property makes sense. One of the intents of properties is to allow a 
> library to upgrade a variable to a set of methods without breaking third 
> party user code. And in the new version of your library, taking the 
> address might make sense or be needed due to new functionality.
>
>> Somewhat related question: What normally happens when you try to get a
>> delegate to an overloaded function?
>
> It grabs the first overload, IIRC.

Then maybe what we need is a way to specify what overload of a function you 
want to get a reference to. Something along these lines (disregard the exact 
syntax, I'm looking more at the semantics):

ref int foo() {/*...*/}  // #1
ref int foo(int x, string y) {/*...*/} // #2
main()
{
    auto a = &foo;  // Either same behavior as now, or disallowed for 
overloaded funcs, or a collection of delegates
    auto b = &foo(); // Address of return value of #1
    auto c = &foo(1, "foo"); // Address of return value of #2
    auto d = &foo(void); // Delegate to #1
    auto e = &foo(int, string);  // Delegate to #2
}

Then, that would open the door for this:

class Foo
{
    @property ref uint foo() {/*...*/}
}
main()
{
    auto f = new Foo();
    auto a = &f.foo;  // Address of returned value
    auto b = &f.foo(void); // Delegate to getter
    //auto c = &f.foo(uint); // If exists, delegate to setter
}




More information about the Digitalmars-d mailing list