alias to struct method

Ali Çehreli acehreli at yahoo.com
Fri Dec 22 19:26:30 UTC 2017


On 12/22/2017 09:53 AM, Marc wrote:
 > How can I create a alias to a struct method?
 >
 >> struct S {
 >>  string doSomething(int n) { return ""; }
 >> }
 >
 > I'd like to do something like this (imaginary code):
 >
 > alias doSomething = S.doSomething;
 >
 > then call it by doSomething(3)

That can't work because there is no S object to call doSomething on. One 
way is to use a delegate:

struct S {
     string doSomething(int n) { return ""; }
}

void main() {
     S s;
     auto d = (int n) { s.doSomething(n); };
     d(3);
}

Ali



More information about the Digitalmars-d-learn mailing list