July 31, 2008 Re: how to do member function pointer in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to newbie | 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
| |||
July 31, 2008 Re: how to do member function pointer in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | > 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? Can fnp be invoked on an actual object? and what's the syntax? Bob bob = new Bob(); fnp(bob); // ? how to make this call? > Do you care if other instances of the same type have pointers to other function? The_Type fnp = Bob.a; | |||
July 31, 2008 Re: how to do member function pointer in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to JAnderson | > 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.
Go back to my original question:
How to create a variable which hold a particular function pointer of a class, and can be invoked on different objects of that class:
fp = &(A.f);
A a1, a2;
a1.fp(); // how to make this invocation work?
a2.fp();
And I have one more requirement that even if A.f() and A.g() have same signature,
fp = &(A.g); // expect compiler report error here.
How to declare the type of such fp?
| |||
July 31, 2008 Re: how to do member function pointer in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to u | u wrote: >>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. > > > Go back to my original question: > > How to create a variable which hold a particular function pointer of a class, and > can be invoked on different objects of that class: > > fp = &(A.f); > A a1, a2; > a1.fp(); // how to make this invocation work? > a2.fp(); > > And I have one more requirement that even if A.f() and A.g() have same signature, > > fp = &(A.g); // expect compiler report error here. > > How to declare the type of such fp? There is a current thread in digitalmars.D.learn titled "Pointer to member variable again." It has turned into a discussion of this very topic. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org | |||
July 31, 2008 Re: how to do member function pointer in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kirk McDonald | == Quote from Kirk McDonald (kirklin.mcdonald@gmail.com)'s article
> u wrote:
> > Go back to my original question:
> >
> > How to create a variable which hold a particular function pointer of a class, and can be invoked on different objects of that class:
> >
> > fp = &(A.f);
> > A a1, a2;
> > a1.fp(); // how to make this invocation work?
> > a2.fp();
> >
> > And I have one more requirement that even if A.f() and A.g() have same signature,
> >
> > fp = &(A.g); // expect compiler report error here.
> >
> > How to declare the type of such fp?
> There is a current thread in digitalmars.D.learn titled "Pointer to
> member variable again." It has turned into a discussion of this very topic.
I had hoped that D will offer better solution than C++'s member function pointer.
Looks like currently it's more difficulty to do what I want in D.
Probably I will live with the wrapper function for now.
| |||
July 31, 2008 Re: how to do member function pointer in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to u | u wrote:
>> 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.
>
> Go back to my original question:
>
> How to create a variable which hold a particular function pointer of a class, and
> can be invoked on different objects of that class:
>
> fp = &(A.f);
> A a1, a2;
> a1.fp(); // how to make this invocation work?
> a2.fp();
>
> And I have one more requirement that even if A.f() and A.g() have same signature,
>
> fp = &(A.g); // expect compiler report error here.
>
> How to declare the type of such fp?
//What about something like:
void funcBind(Obj, alias Func)(Obj obj)
{
obj.Func();
}
...
alias funcBind!(A, f) fp;
A a1, a2;
fp(a1);
fp(a2);
alias funcBind!(A, g) fp; //Compiler error -> Duplicate
//Note with a little more work you could make A implicit
//and therefore have any class Type as the first parameter.
I can't really see why function/member pointers are necessary for this situation since Member pointers are one to many and you seem to want a one-to-one relationship.
-Joel
| |||
July 31, 2008 Re: how to do member function pointer in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to u | Reply to u, >> 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? > Can fnp be invoked on an actual object? and what's the syntax? > the whole thing is hypothetical so ... > Bob bob = new Bob(); > fnp(bob); // ? how to make this call? >> Do you care if other instances of the same type have pointers to >> other function? The_Type fnp = Bob.a; >> | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply