Calling an alias for a Class method in another scope
Jacob Carlborg
doob at me.com
Fri Jun 1 06:43:59 PDT 2012
On 2012-06-01 15:25, d coder wrote:
> Hello Steve, thanks for looking at this.
>
> I see the code works when I create the delegate first and then send it
> to template. That is the way you do it here.
>
> void main() {
> Foo f = new Foo();
> auto dg = &f.foo; // need to make a symbol so it can be aliased
> callfoo!(dg)();
> }
>
>
> But it does not work when I put &f.foo directly as the template
> argument. But it works when I say:
>
> void main() {
> Foo f = new Foo();
> callfoo!(() {f.foo();})(f);
> }
>
> But that still does not solve my problem. In my real code, I want to
> send the delegate to a template class. And I am supposed to do that as a
> member of another class. So the code looks something like:
>
> Class Bar(alias F) {
> // Call F in some function here
> }
>
> Class Foo {
> void foo();
> Bar!(() {foo();}) bar;
> }
>
> Again this does not work. Maybe I am expecting too much from D. :-) I am
> somewhat aware of the issues involved here. I have seen some earlier D
> threads.
>
> But then I thought there might be a way to pass the method function
> literal (foo) and separately the this pointer of Foo object and then
> combine the two at the other end (inside Bar).
>
> Regards
> - Puneet
>
Originally you said "class method", so I was thinking like this:
class Foo
{
static void bar ()
{
writeln("bar");
}
}
void foo (alias m) ()
{
m();
}
void main ()
{
foo!(Foo.bar);
}
But if you actually mean "instance method" you need to use delegates and
possible compose them as Steven showed.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list