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 

Reply via email to