www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Function pointers as values?

I was playing with D's function pointers and immutability and ran into  
some trouble. It's probably because function pointers are treated just  
like data pointers when it comes to immutability. To mend this, function  
pointers should be treated as values. It may sound odd but makes sense --  
you can't possibly dereference a function pointer, so we're dealing only  
with addresses themselves and these are values.

One of the results of the amendment would be allowing implicit conversion  
 from immutable/const to mutable and vice versa. Note that

     immutable(void function()) ifp;
     void function() fp = ifp;   // Error: cannot implicitly convert  
expression (fp) of type immutable(void function()) to void function()

is exactly the same as

     immutable int ii;
     int i = ii;

so should compile.

I was also thinking that an expression taking a function's address (&fun)  
should be an immutable function pointer. Not sure about this one, though.

BTW, a quick bugzilla search shows that I'm not the only one who wants  
function pointers as values.

Jan 18 2010