限制 Ada 中特定其他函数调用的函数
假设我有一个过程,我只想由另一个特定过程调用。是否可以强制限制该过程,使其只能由指定的调用过程引用?我真正想知道的是是否有另一种方法来编写代码,这样您就不必在过程中嵌套/嵌入过程,以强制限制范围。
procedure one
procedure two
begin
blah
end two;
begin
end one;
编辑:顺便说一句,这是 Ada 代码。
Suppose I have a procedure that I want to only have called by another specific procedure. Is it possible to force restrictions on that procedure so that it can only be referenced by the specified calling procedure? Really what I'm wanting to know, is whether there is another way to write the code so you don't have to nest/embed procedures within procedures, to force a limited scope.
procedure one
procedure two
begin
blah
end two;
begin
end one;
EDIT: This is for Ada Code btw.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不(一般来说)。
公共过程是公共过程,因此它可以由任何与其“一起”的对象(如果它是独立过程)或声明它的包来调用。
如果其中任何一种方法适合您的实现方法,则有几种方法可以限制其可见性:
但正如 TED 所说,使用该语言并利用其功能,而不是尝试重新创建其他语言的构造。
No (generally speaking).
A public procedure is a public procedure, so it can be invoked by anything that "with's" it (if it's a standalone procedure) or the package in which it is declared.
There are a few ways to constrain its visibility if any of these might fit with your implementation approach:
But like T.E.D. says, work with the language and exploit its capabilities rather than trying to recreate some other language's constructs.
好吧,如果您将过程一单独放入一个包中,并将过程二放入其私有部分,那么就不会再有其他例程了。能够调用它(除非写入包或子包中)。
您还可以创建一个标记类型,其中包含特定于
过程一
的任何数据,并将过程二
放入其包中,并以该类型的对象作为参数。然后其他人可能会调用过程二
,但不会调用过程一
的对象。我有点困惑为什么你想在不使用作用域的情况下重新创建 Ada 的作用域行为。拥抱语言。
Well, if you were to put
procedure one
in a package by itself and putprocedure two
in itsprivate
section, then no other routine would be able to call it (unless written into the package or a child package).You could also make a tagged type with any data specific to
procedure one
in it, and putprocedure two
in its package with an object of that type as a parameter. Then others might callprocedure two
, but not withprocedure one
's object.I'm a little confused as to why you'd want to recreate Ada's scoping behavior without using scoping though. Embrace the language.
我有两个可能的建议。第一个有点奇怪,有点偏离主题,但我想把它提出来以防您不知道,因为大多数答案都与隐藏代码的可见性或更改关系有关
您可以考虑使用Ada 任务功能并使用“呼叫者属性”。通常这仅用于任务分配,然后“调用者”名称仅表示接收任务的调用任务。但是,一旦进入接收任务的条目,您就可以使用呼叫者姓名快速结束或以其他方式将呼叫者标记为错误或不是您期望的呼叫者。这基本上在任务条目中放置了一个“门卫”,然后它可以决定让他们继续,将调用者重新排队到另一个条目,或者做其他事情。但同样,只有当您有一个任务消耗来自另一个任务的已发布调用时,这才真正起作用。这是我在 Ada 中知道的唯一一件事,您可以在其中检测谁给您打电话并在运行时执行一些操作。
但是你的问题似乎想要使用作用域,所以我同意这里所说的内容,只是补充说,在 Ada 中,有嵌套过程(为了可读性)是正常的,但除此之外,你可以考虑创建子进程包并反向使用层次结构。这就是将子级暴露给程序员,并使父级只能从子级访问。将父级设计为范围非常有限,这样父级的公共规范对于任何没有父级规范私有视图的调用者来说完全毫无价值。这样你就可以进行分离,只有子级可以访问父级中的函数,并且可以实际调用它们,因为他们对父级的类型和函数定义有完整的视图。
祝你的问题好运。
I have two possible suggestions. The first one is slightly odd and off topic a bit but I wanted to bring it up in case you didn't know since most of the answers have to do with hiding visibility of the code or changing relationships
You could consider using the Ada Tasking features and use the 'Caller Attribute. Normally this is only for tasking and then the "caller" name only denotes the calling task to the receiving task. But once inside the receiving task's entry you can then use the Caller name to quickly end or otherwise flag the caller as wrong or not the caller you expected. This basically puts a "doorman" inside the task entry which could then decide to let them proceed, requeue the caller to a different entry, or do something else. But again this would only really work if you have a task consuming published calls from another task. This is the only thing that I'm aware of in Ada where you can detect who called you and do something about it at runtime.
However your question seemed to want to use scope and so I would agree with what's been said here and only add that in Ada it is a normal have have nested procedures (for readability) but in addition to that you could consider creating child packages and using the hierarchy in reverse. That is expose the chidren to the programmer and make the parent only accessible from the children. Design the parent to be very limited in scope such that the public spec of the parent is totally worthless to any caller that doesn't have the private view of the parent's spec. That way you have your separation and only the chidren can access the functions in the parent and can actually call them because they have a complete view of the parent's types and function definitions.
Good luck with your issue.