Ada:运算符不直接可见

Con*_*ine 2 packages ada visible operator-keyword

我正在使用GNAT GPS studio IDE以便在Ada中进行一些培训。我在软件包可见性方面遇到问题。

首先,我在名为“ DScale.ads”的文件中指定一个包含以下类型的包:

package DScale is 
   type DMajor is (D, E, F_Sharp, G, A, B, C_Sharp);
end DScale;
Run Code Online (Sandbox Code Playgroud)

然后,在另一个文件(“ Noteworthy.ads”)中指定一个包,该包定义了将使用DScale包的DMajor类型的过程:

with Ada.Text_IO;
with DScale;

package NoteWorthy is 
   procedure Note;
end NoteWorthy;
Run Code Online (Sandbox Code Playgroud)

最后,在“ Noteworthy.adb”中,提供包“ Noteworthy”的包主体:

with Ada.Text_IO; use Ada.Text_IO;

package body Noteworthy is
   procedure Note is 
      package ScaleIO is new Enumeration_IO(DScale.DMajor);
      thisNote : DScale.DMajor := DScale.D;   
   begin
      ScaleIO.Get(thisNote);

      if thisNote = DScale.DMajor'First then 
         Put_Line("First note of scale.");
      end if;
   end Note;
begin
   null;   
end NoteWorthy;
Run Code Online (Sandbox Code Playgroud)

如果按原样保留代码,则“ Noteworthy”包正文中的“ if thisNote = DScale.DMajor'First then”语句将收到“操作员无法直接看到”错误。

有没有一种方法可以不使用“ use”或“ use type”子句来绕过此错误?

谢谢。

Mar*_*c C 5

Ada 类型不仅仅是对值的描述,它们是带来大量操作的实体,体现为运算符和属性。因此,如果您想直接查看类型的具体“=”运算符,则必须使其可见。为此,您需要“使用”或“使用类型”。

为什么要绕过语言功能?只需明智地使用它。


Jac*_*sen 5

您的问题至少有两个答案。

1:

if DScale."=" (thisNote, DScale.DMajor'First) then
Run Code Online (Sandbox Code Playgroud)

2:

function "=" (Left, Right : DScale.DMajor) return Boolean renames DScale.DMajor;
...
if thisNote = DScale.DMajor'First then
Run Code Online (Sandbox Code Playgroud)

但是,为什么要使用这些选项之一而不是:

use type DScale.DMajor;
...
if thisNote = DScale.DMajor'First then
Run Code Online (Sandbox Code Playgroud)