Note that there are some explanatory texts on larger screens.

plurals
  1. POAda Generic Package with Default Subprogram
    primarykey
    data
    text
    <p>I am trying to create an Ada generic package that has a subprogram parameter with a default value. I cant get the compiler to recognize the default value.. Im guessing this is due to visibility. Is there a way to forward declare a function within the generic declaration?</p> <p>The Generic Spec:</p> <pre><code>generic type Item is private; type Item_Ref is access all Item; Addr : System.Address; Default : Item; with Is_Valid (Obj : Item) return Boolean; -- Forward Declare ** DOES NOT COMPILE function Default_Validate (Data_Ptr : Item_Ref) return Boolean; with function Validate (Data_Ptr : Item_Ref) return Boolean is Default_Validate; package Foo is -- function Default_Validate (Data_Ptr : Item_Ref) return Boolean; function Read_Eeprom return Item; end Foo; </code></pre> <p>The Generic Body:</p> <pre><code>package body Foo is Obj : aliased Item; for Obj'Address use Addr; -- Read Method function Read_Eeprom return Item is begin -- ** Read EEPROM using OBJ ** Validate (Obj'Unchecked_Access); end Read_Eeprom; -- Default Validate Method function Default_Validate (Data_Ptr : Item_Ref) return Boolean is Valid : Boolean; begin Valid := Is_Valid(Data_Ptr.all); if not Valid then Data_Ptr.all := Default; end if; return Valid; end Default_Validate; end Foo; </code></pre> <p>Driver</p> <pre><code>with Foo; procedure Main is MAX_INT : constant Integer := 100; MIN_INT : constant Integer := 0; -- Special / Non-Scaler Type type Pair_Type is record X : Integer; Y : Integer; end record; type Pair_Ref is access all Pair; -- Is Valid function Int_Is_Valid(Int : Integer) return Boolean is begin return (Int &lt;= MAX_INT and Int &gt;= MIN_INT); end Pair_Is_Valid; -- Is Valid function Pair_Is_Valid(Pair : Pair_Type) return Boolean is begin return Pair.X'Valid and Pair.Y'Valid; end Pair_Is_Valid; -- Validate function Pair_Validate(Pair : Pair_Ref) return Boolean is Valid : Boolean := True; begin if not Pair.X'Valid then Pair.X := 0; Valid := False; end if; if not Pair.Y'Valid then Pair.Y := 0; Valid := False; end if; return Valid; end Special_Validate; type Int_Ref is access all Integer; My_Int : Integer; My_Pair : Pair_Type; Default_Pair : Pair_Type := (0,0); package Int_Obj is new Foo (Item =&gt; Integer, Item_Ref =&gt; Int_Ref, Addr =&gt; My_Int'Address, Default =&gt; 0, Is_Valid =&gt; Int_Is_Valid); package Pair_Obj is new Foo (Item =&gt; Pair_Type, Item_Ref =&gt; Pair_Ref, Addr =&gt; My_Pair'Address, Default =&gt; Default_Pair, Is_Valid =&gt; Pair_Is_Valid, Validate =&gt; Pair_Validate); Tmp_Int : Integer; Tmps_Pair : Pair_Type; begin Tmp_Int := Int_Obj.Read_Eeprom; Tmp_Pair := Pair_Obj.Read_Eeprom; end Main; </code></pre> <p>The error Im getting is "end of file expected, file can only have one compilation unit" How can I default a generic subprogram to a function that is a member of the package?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload