Using a macro for a function signature
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Apr 5 06:19:43 PDT 2016
On 05.04.2016 13:35, pineapple wrote:
> alias somelongsignature = int(in int x);
alias somelongsignature = int function(in int);
Or if you want to accept methods and such:
alias somelongsignature = int delegate(in int);
You can name the parameter, but it won't be part of the type.
> int examplefunc0(in int x){
> return x * 2;
> }
>
> somelongsignature testfunc0 = examplefunc1;
somelongsignature testfunc0 = &examplefunc0;
Note the ampersand.
> somelongsignature testfunc1 = somelongsignature {return x + 3};
I don't think there's a way to do this as nicely as you want.
One thing you can do is declare the parameters without types:
somelongsignature testfunc1 = (x) {return x + 3;};
Saves you some typing, especially with complicated parameter types.
Another thing is std.traits.Parameters:
alias F = int function(int, float, string, void*);
import std.traits: Parameters;
F testfunc2 = (Parameters!F args) {return args[0] + 3;};
This way you don't have to type out a long list of parameters, but
`args[0]` isn't as nice as `x`, of course.
There may be a way to transfer the parameter names as well, but I
suspect that's going to be a little more fiddly.
More information about the Digitalmars-d-learn
mailing list