Generic instantiation with constrained subtype

This is a discussion on Generic instantiation with constrained subtype within the ADA forums in Programming Languages category; I've been trying to write some test cases for the Booch Components and came up with this. The test case is written as a generic, to be instantiated with the appropriate Container type. This is fine for the definite containers (ie, ones like No_Problem below) but fails for the Bounded forms, which use the Unconstrained/Constrained technique. This is the example: ----------------------------------------------------------------------- package Instantiation_Problem is type Base is tagged null record; type No_Problem is new Base with null record; type Unconstrained (Constraint : Positive) is new Base with null record; subtype Constrained is Unconstrained (Constraint => 10); generic type T is ...

Go Back   Application Development Forum > Programming Languages > ADA

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-23-2008, 08:59 AM
Simon Wright
Guest
 
Default Generic instantiation with constrained subtype

I've been trying to write some test cases for the Booch Components and
came up with this.

The test case is written as a generic, to be instantiated with the
appropriate Container type. This is fine for the definite containers
(ie, ones like No_Problem below) but fails for the Bounded forms, which
use the Unconstrained/Constrained technique.

This is the example:

-----------------------------------------------------------------------
package Instantiation_Problem is

type Base is tagged null record;

type No_Problem is new Base with null record;

type Unconstrained (Constraint : Positive)
is new Base with null record;

subtype Constrained is Unconstrained (Constraint => 10);

generic
type T is new Base with private;
package G is end G;

package NP is new G (No_Problem);

package F is new G (Constrained);
-- ^--------------------18:24

package U is new G (Unconstrained);
-- ^--------------------21:24

end Instantiation_Problem;
-----------------------------------------------------------------------

and GNAT (GCC 4.3.0) says

instantiation_problem.ads:18:24: constraint on actual is incompatible with formal
instantiation_problem.ads:18:24: instantiation abandoned
instantiation_problem.ads:21:24: actual subtype must be constrained
instantiation_problem.ads:21:24: instantiation abandoned

Am I going to have to rethink this? Have I used the wrong way of
specifying the generic parameter? Is GNAT confused? (note, at 21:24 it's
asking for a subtype, so why complain at 18:24?).

Ideally I'd like it if any solution could work with Ada 95 - these are
the Booch Ada 95 Components!

--S
Reply With Quote
  #2  
Old 08-23-2008, 03:09 PM
Jeffrey R. Carter
Guest
 
Default Re: Generic instantiation with constrained subtype

Simon Wright wrote:
>
> type T is new Base with private;


type T (<>) is new Base with private;

eliminates the compiler errors. It may not be suitable for your needs, however.

--
Jeff Carter
"Go and boil your bottoms."
Monty Python & the Holy Grail
01
Reply With Quote
  #3  
Old 08-23-2008, 04:24 PM
Simon Wright
Guest
 
Default Re: Generic instantiation with constrained subtype

"Jeffrey R. Carter" <spam.jrcarter.not@spam.acm.org> writes:

> Simon Wright wrote:
>>
>> type T is new Base with private;

>
> type T (<>) is new Base with private;
>
> eliminates the compiler errors. It may not be suitable for your needs,
> however.


Hmm, I need to be able to say

generic
type T (<>) is new Base with private;
package G is
O : T;
end G;

Ah! I can provide an initialising value!

generic
type T (<>) is new Base with private;
V : T;
package G is
O : T := V;
end G;

A : Constrained;

package F is new G (Constrained, A);

-- and it works fine! Thanks for the help ..
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 10:00 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.