comp.lang.ada
 help / color / mirror / Atom feed
From: "(see below)" <yaldnif.w@blueyonder.co.uk>
Subject: using interface types: guru assistance begged
Date: Thu, 06 Jul 2006 16:20:21 +0100
Date: 2006-07-06T16:20:21+01:00	[thread overview]
Message-ID: <C0D2E8C5.586FF%yaldnif.w@blueyonder.co.uk> (raw)

I am trying to learn how to use Ada 2005's interface types,
using GNAT GPL 2006 on MacOS X,
with an example along the following lines (much trimmed):

generic
   type a_member_type is (<>);
package generic_sets_interface is

   type a_set is interface;

   function is_empty (trial_set : a_set) return Boolean is abstract;
   pragma Inline(is_empty);
   
   function empty_set return a_set is abstract;
   pragma Inline(empty_set);
   
   function bit (candidate : a_member_type) return a_word_set;
   pragma Inline (bit);
...
end generic_sets_interface;

......

with generic_sets_interface;
generic
   type a_basis_type  is mod <>;
   type a_member_type is (<>);
package generic_word_masks is

   package generic_word_masks_interface is new
generic_sets_interface(a_member_type);
      
   type a_set is new generic_word_masks_interface.a_set with private;

   function is_empty (trial_set : a_word_set) return Boolean;
   pragma Inline(is_empty);
   
   function empty_set return a_word_set;
   pragma Inline(empty_set);
...
private
   type a_word_set is new generic_word_masks_interface.a_set with
      record
         basis : a_basis_type;
      end record;
end generic_word_masks;

......

package body generic_word_masks is

   function shift_right (v : a_basis_type; amount : Natural) return
a_basis_type;
   pragma Import(Convention => Intrinsic,  Entity => shift_right);

   empty_basis : constant a_basis_type := 0;
   
   function is_empty (trial_set : a_word_set) return Boolean is
   begin
      return (trial_set.basis = empty_basis);
   end is_empty;
   
   function empty_set return a_word_set is
   begin
      return a_word_set'(basis => empty_basis);
   end empty_set;
 
   function bit (candidate : a_member_type) return a_word_set is
   begin
      return a_word_set'(basis => shift_left(1,
Natural(a_member_type'Pos(candidate))));
   end bit;
...
end generic_word_masks ;


As written this compiles and runs correctly.
However, I would prefer the derivation of a_word_set to be private, thus:

private with generic_sets_interface;
generic
   type a_basis_type  is mod <>;
   type a_member_type is (<>);
package generic_word_masks is

   type a_word_set is private;
   
   function is_empty (trial_set : a_word_set) return Boolean;
   pragma Inline(is_empty);
   
   function empty_set return a_word_set;
   pragma Inline(empty_set);
...
private
   package generic_word_masks_interface is new
generic_sets_interface(a_member_type);
   
   type a_word_set is new generic_word_masks_interface.a_set with
      record
         basis : a_basis_type;
      end record;
end generic_word_masks;

But when I do this, the instantiation in my test program fails, thus:

           type i32 is mod 2**32;
           type fivetrack is range 0..31;
    13.    package word_masks_32 is new generic_word_masks(i32, fivetrack);
           |
        >>> instantiation error at generic_word_masks.ads:76
        >>> type must be declared abstract or "is_empty" overridden
        >>> "is_empty" has been inherited at generic_word_masks.ads:76,
instance at line 13
        >>> "is_empty" has been inherited from subprogram at
generic_sets_interface.ads:13, instance at generic_word_masks.ads:74,
instance at line 13
        >>> instantiation error at generic_word_masks.ads:76
        >>> type must be declared abstract or "empty_set" overridden
        >>> "empty_set" has been inherited at generic_word_masks.ads:76,
instance at line 13
        >>> "empty_set" has been inherited from subprogram at
generic_sets_interface.ads:16, instance at generic_word_masks.ads:74,
instance at line 13

And so on, for all of the subprograms declared in generic_word_masks.
Can anyone tell me what I am doing wrong here, and whether there is
any way to achieve the desired information hiding?

With thanks.
-- 
Bill Findlay
<surname><forename> chez blueyonder.co.uk





             reply	other threads:[~2006-07-06 15:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-06 15:20 (see below) [this message]
2006-07-06 22:50 ` using interface types: guru assistance begged Randy Brukardt
  -- strict thread matches above, loose matches on Subject: below --
2006-07-07 17:54 (see below)
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox