DIPs icon indicating copy to clipboard operation
DIPs copied to clipboard

Aliases of member functions in template arguments

Open vporton opened this issue 7 years ago • 3 comments

vporton avatar Feb 26 '19 16:02 vporton

Please include the example module as a whole. This compiles and correctly runs, so I'm not sure what you're talking about:

import std.stdio, std.meta;

struct S {
	void f() { }
}

void t(alias f)() { writeln ("hello world"); }
void main()
{	S s;
	alias f = s.f;
    t!(s.f);
}

dukc avatar Feb 27 '19 19:02 dukc

I have edited the DIP to show the error more exactly. Here is a copy of the example with the error:

import std.stdio, std.meta;

struct S {
    void f() { }
}

void t(alias f)() { f(); writeln ("hello world"); }
void main()
{
    S s;
    t!(s.f); // does not compile
}

vporton avatar Feb 27 '19 19:02 vporton

You can already do what you want with a bit extra syntax:

import std.stdio, std.meta;

struct S {
    void f() { }
}

void t(alias f)() { f(); writeln ("hello world"); }
void main()
{
    S s;
    t!(() => s.f); // now compiles
}

Of course, this is slightly more verbose, but personally I think it's better to require explicit syntax here. The reason is that you are creating a closure here (t needs to know the address of S s), which means heap and garbage collector usage. I would not like that to be too invisible.

dukc avatar Mar 05 '19 21:03 dukc