有没有办法以符号通用方式循环调用方法?
考虑两种循环方法、工作时间很晚、代码复杂。简而言之,这两个函数看起来像这样:
void foo(int x)
{
... foo(x);
}
void bar(int x)
{
... foo(x); // oops, I meant "bar"
}
很多时候我希望(而且我仍然希望)有一些像“self”这样的符号调用,所以我可以这样重写它:
void foo(int x)
{
... self(x);
}
void bar(int x)
{
... self(x); // great! this is what i meant --> bar
}
到目前为止我能想到的唯一黑客,将编写通用函数“self”,该函数将在调用检查堆栈上,获取调用者并调用它。但是,不可能将它与继承一起使用:
void inherited(int x)
{
base.self(x);
}
非常感谢您的评论/想法/等:-)
Consider two recurrent methods, late hours, complex code. In short, those two functions look like this:
void foo(int x)
{
... foo(x);
}
void bar(int x)
{
... foo(x); // oops, I meant "bar"
}
There are many times I wished (and I still wish) to have some symbolic call like "self", so I could rewrite it like this:
void foo(int x)
{
... self(x);
}
void bar(int x)
{
... self(x); // great! this is what i meant --> bar
}
So far the only hack I can think of, would be to write general function "self" which would on call check stack, get the caller, and call it. However it would not be possible to use it with inheritance:
void inherited(int x)
{
base.self(x);
}
Thank you very much for your comments/ideas/etc :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
写代码的时候要多注意?我不太明白你为什么想要它,但也许我错过了一些东西。
简而言之,我认为如果不经过反思就无法做到这一点,那么就有点矫枉过正了。
Pay more attention when writing code? I don't quite understand why you want it but maybe I'm missing something.
In short, I don't think you can do it without reflection and at that point it would be overkill.
使用 F# ;)
(如果函数在自身内部递归,则需要将它们标记为
rec
,并且您必须将“and
”相互递归函数放在一起 - 使您的错误成为编译器错误.)Use F# ;)
(which requires functions are marked as
rec
if they recurse within themselves, and you have to "and
" mutually recursive functions together - making your mistakes a compiler error.)