Thread overview | |||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 31, 2012 Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Hi, I was going through some sample code online and came across the following code fragment... listenHttp(settings, &handleRequest); //Where handleRequest is a function My question to you is (as the title says) is the address-of operator (&) really needed here? Wouldn't it be better to consider handleRequest to be a reference to the actual function? I think this will make the system consistent with the way variables work in D. IMO this will bring functions/delegates closer to being first class objects in D. What do you think? Regards, Sandeep Datta. |
May 31, 2012 Re: Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sandeep Datta | On Thu, 31 May 2012 11:36:47 +0200, Sandeep Datta <datta.sandeep@gmail.com> wrote:
> Hi,
>
> I was going through some sample code online and came across the following code fragment...
>
> listenHttp(settings, &handleRequest); //Where handleRequest is a function
>
> My question to you is (as the title says) is the address-of operator (&) really needed here? Wouldn't it be better to consider handleRequest to be a reference to the actual function? I think this will make the system consistent with the way variables work in D. IMO this will bring functions/delegates closer to being first class objects in D.
>
> What do you think?
>
It might be because of historical reasons.
A long time ago, D allowed calling functions without (), so what if handleRequest returns a function?
Should the passed reference be handleRequest or handleRequest()?
This issue is still valid for properties as they can be called without ().
|
May 31, 2012 Re: Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sandeep Datta | On Thu, 31 May 2012 11:36:47 +0200, Sandeep Datta wrote: > Hi, > > I was going through some sample code online and came across the following code fragment... > > listenHttp(settings, &handleRequest); //Where handleRequest is a > function > > My question to you is (as the title says) is the address-of operator (&) really needed here? Wouldn't it be better to consider handleRequest to be a reference to the actual function? I think this will make the system consistent with the way variables work in D. IMO this will bring functions/delegates closer to being first class objects in D. > > What do you think? > > Regards, > Sandeep Datta. It is needed. Consider this example: import std.stdio; /* float handleRequest() { return 1.0f; } */ int handleRequest() { return 200; } // handleRequest() function int main() { int function() fptr; //fptr = handleRequest; // will not work, because it is "understdood" as: // fptr = handleRequest(); fptr = &handleRequest; // This will work if we have only one handleRequest(); // If you uncomment the first one, you are in trouble int val = handleRequest; // calls handleRequest() actualy //listenHttp(settings, fptr); // no need for & because fptr is an object of "int function()" type writeln(val); // OUTPUT: 200 return 0; } // main function -- Dejan Lekic mailto:dejan.lekic(a)gmail.com http://dejan.lekic.org |
May 31, 2012 Re: Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dejan Lekic | On Thu, 31 May 2012 11:58:58 +0200, Dejan Lekic <dejan.lekic@gmail.com> wrote:
> int function() fptr;
> //fptr = handleRequest; // will not work, because it is "understdood"
> as:
> // fptr = handleRequest();
>
Only without -property
|
May 31, 2012 Re: Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Thu, 31 May 2012 11:58:41 +0200, simendsjo <simendsjo@gmail.com> wrote:
> A long time ago, D allowed calling functions without ()
Still does without using -property :)
|
May 31, 2012 Re: Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dejan Lekic | > //fptr = handleRequest; // will not work, because it is "understdood" > as: > // fptr = handleRequest(); > But do we really need this feature? Typing () does not seem to be too much work besides we can use properties if we really need to drop the brackets. And given the fact that properties have well understood use cases (see http://msdn.microsoft.com/en-us/library/bzwdh01d(VS.71).aspx#cpconpropertyusageguidelinesanchor1) I am not sure using functions as properties is such a good idea. > fptr = &handleRequest; // This will work if we have only one > handleRequest(); > // If you uncomment the first one, you are in > trouble Can't we use auto-inferencing here to select the right method since fptr has the required type information? |
May 31, 2012 Re: Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Thursday, 31 May 2012 at 09:58:42 UTC, simendsjo wrote:
> On Thu, 31 May 2012 11:36:47 +0200, Sandeep Datta <datta.sandeep@gmail.com> wrote:
>
>> Hi,
>>
>> I was going through some sample code online and came across the following code fragment...
>>
>> listenHttp(settings, &handleRequest); //Where handleRequest is a function
>>
>> My question to you is (as the title says) is the address-of operator (&) really needed here? Wouldn't it be better to consider handleRequest to be a reference to the actual function? I think this will make the system consistent with the way variables work in D. IMO this will bring functions/delegates closer to being first class objects in D.
>>
>> What do you think?
>>
>
> It might be because of historical reasons.
> A long time ago, D allowed calling functions without (), so what if handleRequest returns a function?
> Should the passed reference be handleRequest or handleRequest()?
> This issue is still valid for properties as they can be called without ().
Thanks for the information! But I think we should use real properties when we need to do that and stop using functions as improvised properties. Please see my response to Dejan Lekic for my argument supporting this claim.
|
May 31, 2012 Re: Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sandeep Datta | "Sandeep Datta" <datta.sandeep@gmail.com> wrote in message news:voifmkyfwlslfpgipvlj@forum.dlang.org... >> //fptr = handleRequest; // will not work, because it is "understdood" >> as: >> // fptr = handleRequest(); >> > > But do we really need this feature? Typing () does not seem to be too much work besides we can use properties if we really need to drop the brackets. And given the fact that properties have well understood use cases (see http://msdn.microsoft.com/en-us/library/bzwdh01d(VS.71).aspx#cpconpropertyusageguidelinesanchor1) I am not sure using functions as properties is such a good idea. > This has been a *HUGE* source of debate. It's D's own little "tabs vs spaces" war. Ultimately, the () is going to be required (unless the function is marked @property, in which case it'll be required that you *don't* use the parens). And as has been said before, it already works this way if you give the -property switch to DMD (and this behavior is planned to become the default...someday). But the only reason any of this is happening at all is because of a specific ambiguity that was discovered with the old "empty parens are optional" approach. |
May 31, 2012 Re: Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | > But the only reason any of this is happening at all is
> because of a specific ambiguity that was discovered with the old "empty
> parens are optional" approach.
Hmm interesting (esp since it works out in favor of what I wanted :) ) but TBH I do not have a problem with leaving the parens out if it does not meddle with the way I'd prefer to use the language. But it seems you can't have the cake and eat it too.
Having said that, what is your opinion on dropping the ampersand? To me it looks antiquated and out of place especially since it conjures up images of unsafe pointers in C/C++.
|
May 31, 2012 Re: Is the address-of operator (&) really needed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sandeep Datta | On Thu, 31 May 2012 07:45:54 -0400, Sandeep Datta <datta.sandeep@gmail.com> wrote:
>> But the only reason any of this is happening at all is
>> because of a specific ambiguity that was discovered with the old "empty
>> parens are optional" approach.
>
> Hmm interesting (esp since it works out in favor of what I wanted :) ) but TBH I do not have a problem with leaving the parens out if it does not meddle with the way I'd prefer to use the language. But it seems you can't have the cake and eat it too.
>
> Having said that, what is your opinion on dropping the ampersand? To me it looks antiquated and out of place especially since it conjures up images of unsafe pointers in C/C++.
If we removed the requirement for the ampersand, along with requiring parentheses for non-property functions, code which expected to call the function without parentheses would silently compile, but not do what was intended.
D aims to avoid making code silently switch behavior, so I don't think we can do this.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation