comp.lang.ada
 help / color / mirror / Atom feed
* RE:  Generics Question
@ 1989-09-19 23:03 Jay Labhart
  0 siblings, 0 replies; 8+ messages in thread
From: Jay Labhart @ 1989-09-19 23:03 UTC (permalink / raw)



Hello,


I am working on a project trying to utilize the generics portion
of Ada.  Here is my problem.

I need to have the user instantiate a package from his procedure
which inturn instantiates many more packages using the users
inputed type.

Here a sample


 generic
  type x is (<>);

  package foo is
   procedure start_it ( s : x);
  end foo;

  package body foo is
   procedure start_it
     ****
      *** here is where I would like to instaniate more packages
  end foo;



I am trying to allow a tool to accept different types within
Ada, rather than utilizing only integer, float...


Your help is greatly appreciated



Jay Labhart
jay@snow-white.merit-tech.com

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

* Generics question
@ 1997-07-22  0:00 Skip Carter
  1997-07-22  0:00 ` W. Wesley Groleau x4923
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Skip Carter @ 1997-07-22  0:00 UTC (permalink / raw)



	I have a computational algorithm that I've implemented that has
	the (admitedly unusual) property that it is perfectly valid for either
	arrays of Integers or arrays of Floats.    As far as I can see I
	CANNOT make a generic version of the package that will work for
	both, e.g.:

	Generic
                Type Element is <something meaning INTEGER or FLOAT subtypes>
                Type DataArray  is Array(Natural  Range<>) of Element;

	.....


	Is there a way to do this ?   or do I have to actually make two different copies
	(which differ only in the definition of ELEMENT).


-- 
 Everett (Skip) Carter        Phone:  408-641-0645 FAX: 408-641-0647
 Taygeta Scientific Inc.      INTERNET: skip@taygeta.com
 1340 Munras Ave., Suite 314  WWW: http://www.taygeta.com/
 Monterey, CA. 93940








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

* Re: Generics question
  1997-07-22  0:00 Generics question Skip Carter
@ 1997-07-22  0:00 ` W. Wesley Groleau x4923
  1997-07-23  0:00 ` Brian Rogoff
  1997-07-23  0:00 ` Robert Dewar
  2 siblings, 0 replies; 8+ messages in thread
From: W. Wesley Groleau x4923 @ 1997-07-22  0:00 UTC (permalink / raw)



>         I have a computational algorithm that I've implemented that has
>         the (admitedly unusual) property that it is perfectly valid for either
>         arrays of Integers or arrays of Floats.    As far as I can see I
>         CANNOT make a generic version of the package that will work for
>         both, e.g.:
> 
>         Generic
>                 Type Element is <something meaning INTEGER or FLOAT subtypes>
>                 Type DataArray  is Array(Natural  Range<>) of Element;
> 
>         .....
> 
>         Is there a way to do this ?   or do I have to actually make two different copies
>         (which differ only in the definition of ELEMENT).

Does it use attributes of type Element?  If not, say
   generic
      type Element is private;
      type Data_Array is Array(Natural  Range<>) of Element;
   ....

when you try to compile, you'll get error messages about undefined
operators.  Something along the lines of "*"(Element,element)return
element is not defined.  When that happens, add

    function "*" ( L, R : Element ) return Element is <>;

to your list of formal parameters.

-- 
----------------------------------------------------------------------
    Wes Groleau, Hughes Defense Communications, Fort Wayne, IN USA
Senior Software Engineer - AFATDS                  Tool-smith Wanna-be
                    wwgrol AT pseserv3.fw.hac.com

Don't send advertisements to this domain unless asked!  All disk space
on fw.hac.com hosts belongs to either Hughes Defense Communications or 
the United States government.  Using email to store YOUR advertising 
on them is trespassing!
----------------------------------------------------------------------




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

* Re: Generics question
  1997-07-22  0:00 Generics question Skip Carter
  1997-07-22  0:00 ` W. Wesley Groleau x4923
  1997-07-23  0:00 ` Brian Rogoff
@ 1997-07-23  0:00 ` Robert Dewar
  2 siblings, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1997-07-23  0:00 UTC (permalink / raw)



Skip Carter says

<<        I have a computational algorithm that I've implemented that has
        the (admitedly unusual) property that it is perfectly valid for either
        arrays of Integers or arrays of Floats.    As far as I can see I
        CANNOT make a generic version of the package that will work for
        both, e.g.:

        Generic
                Type Element is <something meaning INTEGER or FLOAT subtypes>
                Type DataArray  is Array(Natural  Range<>) of Element;
>>

No problem, just make the generic formal type private, and then require
the user (by default if convenient) to pass in the set of operators you
require, i.e. put statements like

   with "+"(L,R : Element) return Element

That will work fine.





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

* Re: Generics question
  1997-07-22  0:00 Generics question Skip Carter
  1997-07-22  0:00 ` W. Wesley Groleau x4923
@ 1997-07-23  0:00 ` Brian Rogoff
  1997-07-24  0:00   ` Michael F Brenner
  1997-07-23  0:00 ` Robert Dewar
  2 siblings, 1 reply; 8+ messages in thread
From: Brian Rogoff @ 1997-07-23  0:00 UTC (permalink / raw)



On 22 Jul 1997, Skip Carter wrote:
> 	I have a computational algorithm that I've implemented that has
> 	the (admitedly unusual) property that it is perfectly valid for either
> 	arrays of Integers or arrays of Floats.    As far as I can see I
> 	CANNOT make a generic version of the package that will work for
> 	both, e.g.:

To do a general numerical algorithm like you describe the Ada 95 idiom is: 

generic 
	type Numeric_Type is private;
	with function "+" (X,Y : Numeric_Type) return Numeric_Type is <>; 
	with function "-" (X,Y : Numeric_Type) return Numeric_Type is <>; 
	with function "*" (X,Y : Numeric_Type) return Numeric_Type is <>; 
	... etc ...
package Numerics is 
-- no body
end;

generic 
	with package Numbers is new Numerics(<>); 
        -- generic formal package parameters
package My_Numerical_Algorithms is 
... insert your stuff here ...
end;

I suppose someone will suggest making the numbers tagged types too, but I 
doubt you want to dispatch and if you make it a tagged type you get
probably double the size of your matrices. 

> 	Is there a way to do this ?   or do I have to actually make two different copies
> 	(which differ only in the definition of ELEMENT).

Note that what I describe easily handles any numeric type you have, with
the caveat that if you end up heap allocating numbers (say bignums) you 
will probably get yourself into trouble unless you deal with finalization.
But Integers, Floats, Complex, Interval, all work fine.

-- Brian






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

* Re: Generics question
  1997-07-23  0:00 ` Brian Rogoff
@ 1997-07-24  0:00   ` Michael F Brenner
  1997-07-24  0:00     ` Jon S Anthony
  1997-07-24  0:00     ` Brian Rogoff
  0 siblings, 2 replies; 8+ messages in thread
From: Michael F Brenner @ 1997-07-24  0:00 UTC (permalink / raw)



    > To do a general numerical algorithm like you describe the Ada 95 idiom is:
    >   generic
    >   type Numeric_Type is private;
    >   with function "+" (X,Y : Numeric_Type) return Numeric_Type is <>;
    >   with function "-" (X,Y : Numeric_Type) return Numeric_Type is <>;
    >   with function "*" (X,Y : Numeric_Type) return Numeric_Type is <>;
    >   ... etc ...
    >   package Numerics is

The etc is not too bad in an unnested generic package, but there is no limit
to how big it can get in nested packages, which is why Ada-200X should
consider passing packages to packages.




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

* Re: Generics question
  1997-07-24  0:00   ` Michael F Brenner
@ 1997-07-24  0:00     ` Jon S Anthony
  1997-07-24  0:00     ` Brian Rogoff
  1 sibling, 0 replies; 8+ messages in thread
From: Jon S Anthony @ 1997-07-24  0:00 UTC (permalink / raw)



In article <5r7cvb$bfb@top.mitre.org> mfb@mbunix.mitre.org (Michael F Brenner) writes:

>     > To do a general numerical algorithm like you describe the Ada 95 idiom is:
>     >   generic
>     >   type Numeric_Type is private;
>     >   with function "+" (X,Y : Numeric_Type) return Numeric_Type is <>;
>     >   with function "-" (X,Y : Numeric_Type) return Numeric_Type is <>;
>     >   with function "*" (X,Y : Numeric_Type) return Numeric_Type is <>;
>     >   ... etc ...
>     >   package Numerics is
> 
> The etc is not too bad in an unnested generic package, but there is no limit
> to how big it can get in nested packages, which is why Ada-200X should
> consider passing packages to packages.

?!?!??  Ada95 supports passing packages to packages (generically) and
one of its intended uses is for just this kind of thing (signatures
with boatloads of operations).

/Jon

-- 
Jon Anthony
OMI, Belmont, MA 02178
617.484.3383
"Nightmares - Ha!  The way my life's been going lately,
 Who'd notice?"  -- Londo Mollari




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

* Re: Generics question
  1997-07-24  0:00   ` Michael F Brenner
  1997-07-24  0:00     ` Jon S Anthony
@ 1997-07-24  0:00     ` Brian Rogoff
  1 sibling, 0 replies; 8+ messages in thread
From: Brian Rogoff @ 1997-07-24  0:00 UTC (permalink / raw)



On 24 Jul 1997, Michael F Brenner wrote:
>     > To do a general numerical algorithm like you describe the Ada 95 idiom is:
>     >   generic
>     >   type Numeric_Type is private;
>     >   with function "+" (X,Y : Numeric_Type) return Numeric_Type is <>;
>     >   with function "-" (X,Y : Numeric_Type) return Numeric_Type is <>;
>     >   with function "*" (X,Y : Numeric_Type) return Numeric_Type is <>;
>     >   ... etc ...
>     >   package Numerics is
> 
> The etc is not too bad in an unnested generic package, but there is no limit
> to how big it can get in nested packages, which is why Ada-200X should
> consider passing packages to packages.

I don't understand. Ada 95 allows you to pass an instantiation of the
package Numerics to another generic package. The "etc" has to be done
because Ada generics must be explicitly instantiated. Could you describe 
your proposal, in "pseudo-Ada", and show how it helps with this problem?

I think that generic package parameters are pretty powerful, especially 
in combination with those signature packages. 

-- Brian







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

end of thread, other threads:[~1997-07-24  0:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-07-22  0:00 Generics question Skip Carter
1997-07-22  0:00 ` W. Wesley Groleau x4923
1997-07-23  0:00 ` Brian Rogoff
1997-07-24  0:00   ` Michael F Brenner
1997-07-24  0:00     ` Jon S Anthony
1997-07-24  0:00     ` Brian Rogoff
1997-07-23  0:00 ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
1989-09-19 23:03 Generics Question Jay Labhart

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