comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: How to declare a generic formal type "covered" by another?
Date: Thu, 01 May 2014 13:59:35 -0700
Date: 2014-05-01T13:59:35-07:00	[thread overview]
Message-ID: <ljucjn$in$1@dont-email.me> (raw)
In-Reply-To: <slrnlm3u5u.i0l.lithiumcat@nat.rebma.instinctive.eu>

On 05/01/2014 12:33 AM, Natasha Kerensikova wrote:
>
> Well maybe the problem here is my limited vocabulary. I used the word
> "constrained" there only to mean "Something := new S" or "Variable : S;"
> are both valid, i.e. objects of that type can be created without
> further information. There must be some way to express that in generics.

As Rosen has pointed out, this is the definite/indefinite idea, expressed in 
generic formals as lacking/having unknown discriminants ["(<>)"].

Most definite subtypes are constrained, and most indefinite, unconstrained, but 
that's not always the case. Consider

subtype VSI is Integer range 0 .. 255;

type VS (Length : VSI := 0) is record
    Value : String (1 .. Length);
end record;

VS is definite, because you can say

S : VS;

but it's also unconstrained, because you can change the discriminant:

S := (Length => 1, Value => "A");
S := (Length => 2, Value => "Hi");

For your uses, perhaps you should consider something like

generic -- GP
    type T (<>) is limited private;
package GP is
    type T_Access is access all T;

    type Ref is private;

    generic -- F
       with function Allocate return T_Access;
    function Allocate return Ref;

    ...
end GP;

Most smart-pointer packages expose the access type; see, for example,

http://www.oopweb.com/Ada/Documents/AdaLinux/Volume/18.html
http://www.adacore.com/adaanswers/gems/gem-97-reference-counting-in-ada-part-1/

HTH.

-- 
Jeff Carter
"C++ is like jamming a helicopter inside a Miata
and expecting some sort of improvement."
Drew Olbrich
51

  parent reply	other threads:[~2014-05-01 20:59 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-01  6:54 How to declare a generic formal type "covered" by another? Natasha Kerensikova
2014-05-01  7:09 ` J-P. Rosen
2014-05-01  7:33   ` Natasha Kerensikova
2014-05-01 13:35     ` J-P. Rosen
2014-05-01 17:56       ` Natasha Kerensikova
2014-05-01 20:59     ` Jeffrey Carter [this message]
2014-05-02  7:58       ` AdaMagica
2014-05-02  8:17         ` Natasha Kerensikova
2014-05-02 15:12         ` Jeffrey Carter
2014-05-02 15:33           ` Dmitry A. Kazakov
2014-05-02 16:00           ` AdaMagica
2014-05-01  9:30 ` Georg Bauhaus
2014-05-01  9:32 ` Georg Bauhaus
2014-05-01  9:33 ` Georg Bauhaus
2014-05-01 16:34 ` Georg Bauhaus
2014-05-01 18:11   ` Natasha Kerensikova
replies disabled

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