Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 19, 2011 Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
If I have an alias F that represents a function, is there any way for me to create a function func() whose signature is exactly the same as that of F? This includes parameter types, return types, and any/all storage modifiers (e.g. const, lazy, scope, etc.). Without this capability, it's impossible to perform perfect generic redirection for functions that pass arguments by reference or in a lazy fashion, right? |
January 19, 2011 Re: Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to %u | %u:
> If I have an alias F that represents a function, is there any way for me to create a function func() whose signature is exactly the same as that of F?
In theory this has to be enough:
typeof(F) F2;
Bye,
bearophile
|
January 19, 2011 Re: Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > In theory this has to be enough: typeof(F) F2;
But in practice, I want to change the body of my function, or possibly add new parameters in the beginning or the end. Does this let me do either of these?
Thank you!
|
January 19, 2011 Re: Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to %u | Is this what you're looking for?: import std.stdio; import std.traits; void test(alias F)() { ReturnType!(F) otherFunc(ParameterTypeTuple!(F)) { typeof(return) result; return result; } } double foo(int x, int y) { double result; return x + y * 2.5; } void main() { test!foo; } |
January 19, 2011 Re: Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
Sorry, here's a more complete example: import std.stdio; import std.traits; void test(alias F)(int x, int y) { ReturnType!(F) otherFunc(ParameterTypeTuple!(F) args) { typeof(return) result; result = args[0] + args[1] * 2.5; return result; } writeln(otherFunc(x, y)); } double foo(int x, int y) { double result; return x + y * 2.5; } void main() { test!(foo)(4, 5); } |
January 19, 2011 Re: Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
Better: void test(alias F)(ParameterTypeTuple!(F) args) { ReturnType!(F) otherFunc(ParameterTypeTuple!(F) args) { typeof(return) result; result = args[0] + args[1] * 2.5; return result; } return otherFunc(args); } double foo(int x, int y) { double result; return x + y * 2.5; } void main() { writeln(test!(foo)(4, 5)); } |
January 19, 2011 Re: Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
Holy cow is that a bug? It's returning a value even though it's a void function. |
January 19, 2011 Re: Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
Never mind my stupid build script didn't recompile and it ran an old version. Fixed: import std.stdio; import std.traits; auto test(alias F)(ParameterTypeTuple!(F) args) { ReturnType!(F) otherFunc(ParameterTypeTuple!(F) args) { typeof(return) result; result = args[0] + args[1] * 2.5; return result; } return otherFunc(args); } double foo(int x, int y) { double result; return x + y * 2.5; } void main() { writeln(test!(foo)(4, 5)); } |
January 19, 2011 Re: Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 2011-01-19 16:29, Andrej Mitrovic wrote: > Is this what you're looking for?: > > import std.stdio; > import std.traits; > > void test(alias F)() > { > ReturnType!(F) otherFunc(ParameterTypeTuple!(F)) > { > typeof(return) result; > return result; > } > } > > double foo(int x, int y) > { > double result; > return x + y * 2.5; > } > > void main() > { > test!foo; > } Will that keep: ref, out, inout and so on in the signature ? -- /Jacob Carlborg |
January 19, 2011 Re: Exactly Replicating a Function's Signature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | I never thought of that. There's a ParameterStorageClassTuple template, but I don't see a template that combines both of these templates to duplicate the exact signature. Something like that should probably be added to Phobos. |
Copyright © 1999-2021 by the D Language Foundation