comp.lang.ada
 help / color / mirror / Atom feed
* Question about generics.
@ 2006-07-02 16:08 Peter C. Chapin
  2006-07-02 18:49 ` Martin Krischik
                   ` (3 more replies)
  0 siblings, 4 replies; 25+ messages in thread
From: Peter C. Chapin @ 2006-07-02 16:08 UTC (permalink / raw)


Here is what I'm trying to do

generic
  Size : in Integer;
package Xyzzy is
  type Index is mod Size;
  -- etc.
end Xyzzy;

The compiler (gnat) complains about the modular type definition, saying 
that Size is a non-static expression and that's no good. I understand 
what this error means and I understand the rationale behind it (thanks 
to looking in Cohen's book "Ada As a Second Language"). My question is: 
how can I get the effect I'm looking for? My plan is to only instantiate 
the package with constants so all the necessary information should be 
available at compile time. For example

    	package Fizzle is new Xyzzy(Size => 10);

I realize that I could make the Index type a generic parameter but that 
would require me to define the type at the point of instantiation and 
that seems unnatural. Conceptually I'm trying to parameterize the 
package on a (static) size. It doesn't seem like I should have to define 
a type to express that concept.

I admit that I'm a C++ programmer and I'm used to using non-type 
parameters in templates to obtain this effect.

template< int size >
class Xyzzy {
  char array[size];  // size is a compile time constant.
};

Peter



^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-09-02 17:45 Stef  Van Vlierberghe
  0 siblings, 0 replies; 25+ messages in thread
From: Stef  Van Vlierberghe @ 1993-09-02 17:45 UTC (permalink / raw)


In article <CBLyxC.64x@crdnns.crd.ge.com> groleau@e7sa.crd.ge.com (Wes
Groleau x1240 C73-8) writes:

> P.S.  I have seen several "free"  variable  string  packages that have
> what is to me a RIDICULOUS  usage of access types, requiring all sorts
> of contortions by client packages to avoid dangling  pointers and heap
> exhaustion.  My package  and the ones I'm  complaining  about make the
> type private.

Wes, I believe that  introducing an arbitrary  limit is perfectly o.k.
in many situations, but it's just a technique that (in practice) won't
scale up.  If, rather than a simple  string,  you are  implementing  a
multi-file  screen  editor,  you might  want a list of  buffers,  each
buffer  is a list of lines and each line is a list of  characters.  My
favorite  editor has a 64K line length  limit, I have  already  opened
files of 160_000 lines, and often I had more than 20 files open at the
time (of  course I  *never*  hit all  limits in all  lists at the same
time).

The  technique  you  describe is elegant and simple, but I don't think
your  environment  is strong  enough to implement  my favorite  editor
(although  theoretically speaking, it could).  The packages with those
contortions  (typically) will scale up !  As most of the cost of these
contortions  is getting to understand the mechanism, one might as well
bite the bullet.

The *real* fun will obviusly start when we get a 9X compiler,  then we
will  be able to use  the  User-Defined  assignment  and  Finalization
(which I am so  terribly  fond  of) to  transparently  manage  all the
memory  without  any  help  from  the  client  code  (it  just  sees a
non-limited private type).

Where I live, we have terribly  suffered  from this  difficult  choice
between simple techniques that don't scale up and contortions that do,
but  are  quite  complex   indeed.  That's  why  I  did  my  share  of
complaining when the Mapping Specification didn't include User-Defined
Assignment,  and also why I can't praize the Mapping  Team enough, now
that this feature is in !

Halleluia ! God save the Mapping Team !

Thanks Tuck and Bob, you're heroes !

--------------------------------------------------------------------------
Stef VAN VLIERBERGHE            Eurocontrol - Central Flow Management Unit
stef@cfmu.eurocontrol.be        Avenue des Arts 19H
Tel: +32 2 729 33 42            B-1040 BRUSSELS
Fax: +32 2 729 32 16            Belgium

^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-08-12 15:18 Robert I. Eachus
  0 siblings, 0 replies; 25+ messages in thread
From: Robert I. Eachus @ 1993-08-12 15:18 UTC (permalink / raw)


      If you want to create a (ragged) array of strings, you need to
have different discriminant values.  But the rule in 3.7.2(8) says
that the default discriminant is used if there is no constraint in the
subtype indication for the array component, so all components of an
array have the same constraints.  (There is a note at 3.6.1(16)
pointing this out, but it really should be in 3.6...)  In any case by
having a nesting of record types, the discriminant (of the record
inside) of each array component can be different.

      An alternate approach is to have a record containing a current
size and a string.  The disadvantage is that now equality for objects
of the type doesn't work right unless you explicitly set the "unused"
bytes to a specific value.  I prefer the first approach as there is no
distributed cost.


--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...

^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-08-11 18:48 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.
  0 siblings, 0 replies; 25+ messages in thread
From: cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland. @ 1993-08-11 18:48 UTC (permalink / raw)


In article <EACHUS.93Aug10105310@spectre.mitre.org> eachus@spectre.mitre.org (R
obert I. Eachus) writes:
>In article <9308091429.aa12174@Paris.ics.uci.edu> kanderso@mabillon.ICS.UCI.ED
U (Kenneth Anderson) writes:
>  > I.E. I need to be able to say something like
>  >  package Generic_Package_Support is
>  >     Viewers : array (1 .. 5) of STRING := ("How", "Would", "I", "Do", "Thi
s?");
>  >  end Generic_Package_Support;
>
>    You apparently need a package which implements varying strings in
>a way that allows you to have arrays of differently sized strings.
>This can be done in Ada by having a type which has a discriminated
>record (with defaults) wrapped in a record with no discriminants.
>I can send you such a package if you don't have one...

Why the wrapper?  Why won't the following work?

package VAR_STRING is

   Max : constant := 1000; -- substitute your idea of reasonable value

   type STRING_LENGTH_TYPE is range 0 .. Max;

   subtype STRING_INDEX_TYPE is STRING_LENGTH_TYPE range 1 .. Max;

   type TEXT ( Size : STRING_LENGTH_TYPE := 0 ) is private;

   -- all the operations, including conversion functions to and from
   -- STRING, concatenation, etc. two of which are

   function TEXT_OF ( Str : STRING ) return TEXT;

   function STRING_OF ( Txt : TEXT ) return STRING;

   --

end VAR_STRING;

-----------

......

    package INIT is new I_FORGOT_THE_NAME
            ( ( VAR_STRING.TEXT_OF ( "first"  ),
                VAR_STRING.TEXT_OF ( "second" ),
                ...
                VAR_STRING.TEXT_OF ( "last"   ) )  )
.......

Inside the generic you have

  for I in Formal'RANGE loop
    DO_SOMETHING_WITH ( VAR_STRING.STRING_OF ( Formal ( I ) ) );
  end loop;

Although I have not used the VAR_STRING package to pass OBJECTS to a 
generic (except indirectly), I don't see why there should be a problem.

Wes G.

P.S. I have seen several "free" variable string packages that have what is
to me a RIDICULOUS usage of access types, requiring all sorts of contortions
by client packages to avoid dangling pointers and heap exhaustion.  My package
and the ones I'm complaining about make the type private.  In my case, no one
needs to know the fact that it's a simple discriminated record, and "garbage
collection" is done by leaving scope (of a TEXT object).  In the other(s),
the type is private in name only, because the client has to understand the
implementation to protect itself )or carefully follow VERY detailed rules
in the comments in the package spec.  Why are such packages popular, while my
approach is unpopular enough that I couldn't find anything similar in the
repositories and books I checked?  Is there something I don't know?
I have yet to find a disadvantage to it, having used it in user interfaces
and a primitive language parser.  I'm admittedly a bit egotistical when it
comes to my programming skills, but not so much so that I don't get nervous
when it appears that no one else has thought of something so simple.
Robert Eachus's post is the first hint I've seen that I'm not alone.

Huh? A P.S. longer than the "main" ?

^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-08-11  0:25 agate!howland.reston.ans.net!europa.eng.gtefsd.com!darwin.sura.net!seas.g
  0 siblings, 0 replies; 25+ messages in thread
From: agate!howland.reston.ans.net!europa.eng.gtefsd.com!darwin.sura.net!seas.g @ 1993-08-11  0:25 UTC (permalink / raw)


In article <EACHUS.93Aug10105310@spectre.mitre.org> eachus@spectre.mitre.org (R
obert I. Eachus) writes:

[deleted]

>
>  > I.E. I need to be able to say something like
>
>  >  package Generic_Package_Support is
>  >     Viewers : array (1 .. 5) of STRING := ("How", "Would", "I", "Do", "Thi
s?");
>  >  end Generic_Package_Support;
>
>    You apparently need a package which implements varying strings in
>a way that allows you to have arrays of differently sized strings.
>This can be done in Ada by having a type which has a discriminated
>record (with defaults) wrapped in a record with no discriminants.
>I can send you such a package if you don't have one...
>
If I understand this right, we'd better put a warning here to be REAL
careful what the range of the discriminant is, for reasons that we
explain here every couple of months.

To do this ragged array in a more natural way, you'll need 9X.

Mike Feldman

^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-08-10 15:53 Robert I. Eachus
  0 siblings, 0 replies; 25+ messages in thread
From: Robert I. Eachus @ 1993-08-10 15:53 UTC (permalink / raw)


In article <9308091429.aa12174@Paris.ics.uci.edu> kanderso@mabillon.ICS.UCI.EDU
 (Kenneth Anderson) writes:

  > I need a generic package to do some initialization based on an array of
  > strings.

  > I.E. I need to be able to say something like

  >  package Generic_Package_Support is
  >     Viewers : array (1 .. 5) of STRING := ("How", "Would", "I", "Do", "This
?");
  >  end Generic_Package_Support;

    You apparently need a package which implements varying strings in
a way that allows you to have arrays of differently sized strings.
This can be done in Ada by having a type which has a discriminated
record (with defaults) wrapped in a record with no discriminants.
I can send you such a package if you don't have one...

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...

^ permalink raw reply	[flat|nested] 25+ messages in thread
* question about generics
@ 1993-08-09 21:29 Kenneth Anderson
  0 siblings, 0 replies; 25+ messages in thread
From: Kenneth Anderson @ 1993-08-09 21:29 UTC (permalink / raw)


I need a generic package to do some initialization based on an array of
strings.

I.E. I need to be able to say something like

package Generic_Package_Support is

   Viewers : array (1 .. 5) of STRING := ("How", "Would", "I", "Do", "This?");

end Generic_Package_Support;

with Generic_Package;
with Generic_Package_Support;

package my_instantiated_package is
  new Generic_Package(Generic_Package_Support.Viewers);


Then in my generic package, I would loop through the members of the
array and do some initialization.

My problem : How do I specify Generic_Package to accept something like
             this?

Ideas/Suggestions welcome...

Thanks in advance,

Ken Anderson

^ permalink raw reply	[flat|nested] 25+ messages in thread
* Question about generics
@ 1989-05-29 20:54 "14827_DAVID PAPAY"
  0 siblings, 0 replies; 25+ messages in thread
From: "14827_DAVID PAPAY" @ 1989-05-29 20:54 UTC (permalink / raw)


In issue #141, Jonathan Owen presents the following generic unit and asks:

>   generic
>       Max_len : Integer;
>   package VSTR is
>
>      subtype Len_range is integer range 0..Max_len;
>
>      type V_string( len : Len_range := 0 ) is
>          record
>             data : String(1..len);
>          end record;
>
>      ----------------------
>      -- VSTRING SERVICES --
>      ----------------------
>
>      function V_string_of( str : in String ) return V_string;
>
>      Etc...
>end VSTR;
>
>
>When transferring the code to Verdix Ada 3.0, on the apollo (SR 10.1),
>I received a compiler warning on a variable of such a type, saying
>that there is not enough storage for such a variable.
>It seems that the upper limit of len_range is not considered constant...
>
>Any ideas to overcome this difficulty?


First of all, the difficulty has nothing to do with the fact the generics are
being used.  Instead, it arises because of the use of a default expression
for the discriminant in the record type definition:
>
>      type V_string( len : Len_range := 0 ) is
>          record
>             data : String(1..len);
>          end record;
>

Since the discriminant has a default expression, it becomes possible to declare
record objects in two ways:

MY_STRING_A  : V_STRING (20);  -- with an explicit discriminant constraint

MY_STRING_B  : V_STRING;       -- using the default expression of 0.

The first record object is said to be CONSTRAINED.  The value of the
discriminant LEN cannot be changed, (not even with a complete record 
assignment), and evaluation of the attribute MY_STRING_A'CONSTRAINED will be 
TRUE.  Because the language rules do not allow MY_STRING_A.LEN to be changed,
the compiler can allocate the exact (minimum) storage space needed to represent
the object.

In the other hand, the record object MY_STRING_B is UNCONSTRAINED (evaluation
of MY_STRING_B'CONSTRAINED will be FALSE).  Because no explicit discriminant 
constraint was given, the value of MY_STRING_B.LEN can be changed during program
execution (using a complete record assignment only).  Since the discriminant can
change, the compiler cannot allocate the exact storage space needed, but must
allocate enough space to accommodate the largest possible value of LEN.  In 
this case, the record object must be large enough to hold a component DATA of
type STRING with a range of 1..INTEGER'LAST.  Depending on the value of
INTEGER'LAST, this can be quite a long string!

If you wish to use this package on Verdix Ada, I would suggest that you remove 
the default expression " := 0 " from your record type definition and always 
declare record objects of this type with explicit discriminant constraints.


David Papay                         papayd@gtewd.af.mil
GTE Government Systems           
PO Box 7188  M/S 5G09               voice: (415) 694-1522
Mountain View, Ca 94039

^ permalink raw reply	[flat|nested] 25+ messages in thread
* Question about generics
@ 1989-05-29  7:02 "Jonathan B. Owen"
  0 siblings, 0 replies; 25+ messages in thread
From: "Jonathan B. Owen" @ 1989-05-29  7:02 UTC (permalink / raw)


A while back I tried to define the following:

   generic
       Max_len : Integer;
   package VSTR is

      subtype Len_range is integer range 0..Max_len;

      type V_string( len : Len_range := 0 ) is
          record
             data : String(1..len);
          end record;

      ----------------------
      -- VSTRING SERVICES --
      ----------------------

      function V_string_of( str : in String ) return V_string;

      Etc...
end VSTR;

The purpose of such a package is obviously to use Variable length
strings having different maximum lengths.

Using the about definition after an instantiation on Vax-Ada worked fine
(Such as "package VSTR80 is new VSTR(80)" ).

When transferring the code to Verdix Ada 3.0, on the apollo (SR 10.1),
I received a compiler warning on a variable of such a type, saying
that there is not enough storage for such a variable.
It seems that the upper limit of len_range is not considered constant...

Any ideas to overcome this difficulty?

                                             Thank you,
                                                       JB

______________________________________________________________________________
  (--)    /--)     /-(\                 Email: gdau100@bguvm (bitnet)
  \ /    /--K      | \|/\   /\/) /|-\   Snail: 55 Hovevei Zion
  _/_/o /L__)_/o \/\__/  \X/  \_/ | |_/        Tel-Aviv, 63346  ISRAEL
 (/        Jonathan B. Owen             Voice: (03) 281-422

 Point of view:  A chicken is the means by which an egg reproduces an egg.
______________________________________________________________________________

^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2006-07-05 12:08 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-02 16:08 Question about generics Peter C. Chapin
2006-07-02 18:49 ` Martin Krischik
2006-07-03  6:30 ` Jeffrey R. Carter
2006-07-03 10:33   ` Peter C. Chapin
2006-07-03 11:42     ` Jean-Pierre Rosen
2006-07-03 16:44     ` Pascal Obry
2006-07-04  1:09       ` Peter C. Chapin
2006-07-04  6:17         ` M E Leypold
2006-07-04 10:48           ` Peter C. Chapin
2006-07-03 20:03     ` Jeffrey R. Carter
2006-07-03 20:18       ` Dmitry A. Kazakov
2006-07-04  0:08         ` Randy Brukardt
2006-07-04  7:48           ` Dmitry A. Kazakov
2006-07-04  0:43         ` Jeffrey R. Carter
2006-07-03  9:46 ` Martin Krischik
2006-07-04 13:29 ` Stephen Leake
2006-07-05 12:08   ` Dmitry A. Kazakov
  -- strict thread matches above, loose matches on Subject: below --
1993-09-02 17:45 question " Stef  Van Vlierberghe
1993-08-12 15:18 Robert I. Eachus
1993-08-11 18:48 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.
1993-08-11  0:25 agate!howland.reston.ans.net!europa.eng.gtefsd.com!darwin.sura.net!seas.g
1993-08-10 15:53 Robert I. Eachus
1993-08-09 21:29 Kenneth Anderson
1989-05-29 20:54 Question " "14827_DAVID PAPAY"
1989-05-29  7:02 "Jonathan B. Owen"

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