Search code examples
packageoperator-keywordadavisible

Ada : operator is not directly visible


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.


Solution

  • 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