how to do member function pointer in D?

BCS ao at pathlink.com
Wed Jul 30 16:16:57 PDT 2008


Reply to Newbie,

>> 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?
> 

don't ask me.

>> //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 ...
> 

but neither of those will be usefull unless you can assign more than one 
value to the type

what I'm reading (and I must be missing somthing) is somthing like a hash 
table where the keys are the number 5 to the values are the number 3.

One idea on what I'm missing; 

Do you want nothing at all to be assigname to the variable after the fist 
assigment?

class Bob {void a(){} void b(){} }

The_Type fnp = Bob.a;
fnp = Bob.a; // valid or not?

Do you care if other instances of the same type have pointers to other function?

The_Type fnp = Bob.a;


> 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
> 

IIRC that is with regards to program internals, not the external interface

>> 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.
> 





More information about the Digitalmars-d mailing list