how to do member function pointer in D?

JAnderson ask at me.com
Wed Jul 30 21:46:59 PDT 2008


newbie wrote:
>> same solution as befor (the issue is that the only thing you can put after
>> the '=' ouside of a function is a constant expression and a function litteral
>> isn't one)
> 
> Why a function literal is not a constant expression?
> 
> Especially after I defining a global top level wrapper function in this case.
> Under the hood, it should just be a raw pointer to some memory address, why this
> cannot be a constant?
> 
>> //at global scope (OTOH static's are globals)
>> static invariant FP mfp1;
>> static this()
>> {
>> mfp1 = cast(invariant FP)(function void(A obj){obj.f();});
>> }
> 
> Thanks again. I can live with this workaround right now, but I'd really like to
> write it as simple as:
> 
> invariant FP mfp2 = function void(A obj) {obj.f();};  // without the cast
> 
>> I still don't get it, what type of manipulation (outside self modifying code)
> 
> E.g. save in a hash-table, or use them as keys ...
> 
> And I'd really like to do self modifying code ;-) I read somewhere that D plan
> have to AST macros? so we are closer to lisp:
> http://en.wikipedia.org/wiki/Greenspun%27s_Tenth_Rule
> 
>> can you do that doesn't allow switching in a different function with the
>> same signature?
> 
> Yes, I do it manually (by writing program carefully). I want the help from the
> type system (or the language compiler) to protect me from such violation.
> 

I'm not sure I completely understand either but I'll take a stab.  What 
about using polymorphism?  Where delegates can be any type of object 
interfaces restrict what sort of objects can be used with your hash-table.

-Joel



More information about the Digitalmars-d mailing list