How to put overloaded functions in separate files?

Assume two overloaded functions with specification (in package “fu.ads”):

function fu1 return Integer;
function fu1 return Float;

And in the implementation (fu.adb):
function fu1 return Integer is separate;
function fu1 return Float is separate;

I have learned that “fu1” will be stored in file “fu-f1.adb” - but when there are two “fu1”?

function fu1_d return Float is separate;
function fu1 return Float renames fu1_d;

GNAT will expect the body of a separate unit by default to be in files with such names, but that has nothing to do with the language. Ada requires that separate units have unique names, regardless of where or how they are stored, and what compiler you use.

That is a common misinterpretation that file names have to be like they are used with GNAT. And even GNAT can deviate from this scheme.
Ada as the language has nothing to say about how compilation units are stored!

2 Likes

If you aren’t worried about the two functions being in the same separated file, then you can also do something like this

fu.ads

package fu_functions is
   function fu1 return Integer;
   function fu1 return Float;
end package fu_functions ;

fu.adb

package body fu is
   package fu_functions is
      function fu1 return Integer;
      function fu1 return Float;
   end package fu_functions ;

   package body fu_functions is separate;

   function fu1 return Integer renames fu_functions.fu1;
   function fu1 return Float renames fu_functions.fu1;
end fu;

fu-fu_functions.adb

separate
   (fu)
package body fu_functions is
   function fu1 return Integer is (0);
   function fu1 return Float is (0.0)
end package fu_functions ;

If you truly want each fu1 in a completely separate file then I do like Nordic_Dogsledding suggested

fu.ads

package fu_functions is
   function fu1 return Integer;
   function fu1 return Float;
end package fu_functions ;

fu.adb

package body fu is
   function fu1_integer return Integer is separate;
   function fu1_float   return Float is separate;


   function fu1 return Integer renames fu1_Integer;
   function fu1 return Float renames fu1_float;
end fu;

fu-fu1_integer.adb

separate
   (fu1)
function fu1_integer return Integer is (0);

fu-fu1_float.adb

separate
   (fu1)
function fu1_float return Float is (0.0);
2 Likes