digitalmars.D.learn - Get body of a function as string
- John Colvin (2/2) Jun 28 2013 Is there any way of getting the body of a function as a string?
- bearophile (5/8) Jun 28 2013 I think that currently there isn't a simple way to do it. What is
- John Colvin (7/15) Jun 28 2013 I want to create a function with an identical body but different
- Namespace (4/4) Jun 28 2013 And why don't you call the function from your clone function?
- John Colvin (5/9) Jun 28 2013 Because the body of the new function needs to see the parameters
- bearophile (7/9) Jun 28 2013 I think to curry a function all you need to know is its
- monarch_dodra (29/38) Jun 28 2013 Oh, hey! I remember participating in writing some of that :)
- Namespace (1/2) Jun 29 2013 Only Involved? You've written it. I've added only a few things. :)
- Jacob Carlborg (5/7) Jun 29 2013 I remember someone someone modified DMD and added a .codeof property or
- bearophile (6/8) Jun 29 2013 If there are enough use cases for it, then perhaps it's worth
- JS (2/10) Jul 04 2013 There is, or at least there will be!
Is there any way of getting the body of a function as a string? (Obviously only when the source code is available to the compiler)
Jun 28 2013
John Colvin:Is there any way of getting the body of a function as a string? (Obviously only when the source code is available to the compiler)I think that currently there isn't a simple way to do it. What is your use case? Bye, bearophile
Jun 28 2013
On Friday, 28 June 2013 at 13:18:39 UTC, bearophile wrote:John Colvin:I want to create a function with an identical body but different parameters: e.g. given a function int foo(int a){ return a+1; } automatically create a new function int foo(int a)(){ return a+1; } I'm trying to implement a sort of automatic compile-time currying.Is there any way of getting the body of a function as a string? (Obviously only when the source code is available to the compiler)I think that currently there isn't a simple way to do it. What is your use case? Bye, bearophile
Jun 28 2013
And why don't you call the function from your clone function? Maybe this could help you: http://dpaste.1azy.net/fork/597affd2 I used it to generate my own rvalue functions because of the lack of rvalue references.
Jun 28 2013
On Friday, 28 June 2013 at 13:55:54 UTC, Namespace wrote:And why don't you call the function from your clone function?Because the body of the new function needs to see the parameters as known at compile-time.Maybe this could help you: http://dpaste.1azy.net/fork/597affd2 I used it to generate my own rvalue functions because of the lack of rvalue references.Thanks, that was helpful for a few hints, but it doesn't fix this particular problem.
Jun 28 2013
John Colvin:Because the body of the new function needs to see the parameters as known at compile-time.I think to curry a function all you need to know is its signature. And in std.traits probably there is all the functionality to see all kinds of function arguments, their names, tags, etc. Bye, bearophile
Jun 28 2013
On Friday, 28 June 2013 at 20:50:55 UTC, John Colvin wrote:On Friday, 28 June 2013 at 13:55:54 UTC, Namespace wrote:Oh, hey! I remember participating in writing some of that :) Yeah, with traits, you can get enough info to extract all the information you want about the function to redeclare it any way you want. The only thing you *can't* get is... the body! If you have access to the source code of the function, you could redeclare it as a token string? eg: before: int foo(int a) { return a+1; } after: enum fooString = q{ return a+1; }; int foo(int a){mixin(fooString);} int foo()(int a){mixin(fooString);} Yeah... not a great solution. -------- The only way I could see it happen would be with a compile __trait ? Does the compiler even have the info, or is it completely obliterated after the lex phase? The compiler *should* have the info, since it can pinpoint the coordinates of compilation errors. But I wouldn't know too much.And why don't you call the function from your clone function?Because the body of the new function needs to see the parameters as known at compile-time.Maybe this could help you: http://dpaste.1azy.net/fork/597affd2 I used it to generate my own rvalue functions because of the lack of rvalue references.Thanks, that was helpful for a few hints, but it doesn't fix this particular problem.
Jun 28 2013
Oh, hey! I remember participating in writing some of that :)Only Involved? You've written it. I've added only a few things. :)
Jun 29 2013
On 2013-06-28 14:46, John Colvin wrote:Is there any way of getting the body of a function as a string? (Obviously only when the source code is available to the compiler)I remember someone someone modified DMD and added a .codeof property or similar. It was fairly easy. -- /Jacob Carlborg
Jun 29 2013
Jacob Carlborg:I remember someone someone modified DMD and added a .codeof property or similar. It was fairly easy.If there are enough use cases for it, then perhaps it's worth putting both the enhancement request for ".codeof" and its relative patch in Bugzilla. Bye, bearophile
Jun 29 2013
On Saturday, 29 June 2013 at 10:38:57 UTC, bearophile wrote:Jacob Carlborg:There is, or at least there will be!I remember someone someone modified DMD and added a .codeof property or similar. It was fairly easy.If there are enough use cases for it, then perhaps it's worth putting both the enhancement request for ".codeof" and its relative patch in Bugzilla. Bye, bearophile
Jul 04 2013