Ada : operator is not directly visible

 ̄綄美尐妖づ 提交于 2019-12-11 02:23:17

问题


I'm using GNAT GPS studio IDE in order to train a bit in Ada. I'm having an issue with package visibility.

First I specify a package in a file called "DScale.ads" containing a type:

package DScale is 
   type DMajor is (D, E, F_Sharp, G, A, B, C_Sharp);
end DScale;

Then I specify in a different file ("Noteworthy.ads") a package that defines a procedure that will use the DMajor type of the DScale package:

with Ada.Text_IO;
with DScale;

package NoteWorthy is 
   procedure Note;
end NoteWorthy;

Finally in "Noteworthy.adb" I provide the package body for the package "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;

If I leave the code as-is, I will get an "operator not directly visible" error for the "if thisNote = DScale.DMajor'First then" statement in the body of the "Noteworthy" package.

Is there a way to bypass this error without using a "use" or "use type" clause?

Thank you.


回答1:


There are (at least) two answers to your question.

1:

if DScale."=" (thisNote, DScale.DMajor'First) then

2:

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

But why would you use one of those options instead of:

use type DScale.DMajor;
...
if thisNote = DScale.DMajor'First then



回答2:


Ada types are much more than just descriptions of the values, they are entities that bring into existence a whole slew of operations, embodied as both operators and attributes. So if you want direct visibility to a type's concrete "=" operator, you have to make it visible. For that you need either "use" or "use type".

Why bypass a language feature? Simply use it wisely.




回答3:


A couple of points that are related to the structure of your code.

The package spec for Noteworthy doesn't need to "with DScale" as there is nothing in the spec that refers to any feature in that package (maybe you plan to in the future).

The use of enumeration_io is good if you want to simply want to do I/O to files to be read by a computer, but I would never use it for human interaction (apart from quick hacks). The input values it accepts is dictated by Ada's grammar, and typically not what user's expect.



来源:https://stackoverflow.com/questions/25591500/ada-operator-is-not-directly-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!