有没有办法以符号通用方式循环调用方法?

发布于 2024-10-22 22:24:20 字数 527 浏览 0 评论 0原文

考虑两种循环方法、工作时间很晚、代码复杂。简而言之,这两个函数看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2024-10-29 22:24:20

写代码的时候要多注意?我不太明白你为什么想要它,但也许我错过了一些东西。

简而言之,我认为如果不经过反思就无法做到这一点,那么就有点矫枉过正了。

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.

空‖城人不在 2024-10-29 22:24:20

使用 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文