comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: [Newbie] doubly constrained array, dumb question
Date: Tue, 27 Feb 2018 10:18:10 +0100
Date: 2018-02-27T10:18:10+01:00	[thread overview]
Message-ID: <p737oh$102b$1@gioia.aioe.org> (raw)
In-Reply-To: 473f9b1a-6466-4745-9041-107f54062cf2@googlegroups.com

On 27/02/2018 02:29, Mehdi Saada wrote:

> For 8: a class has any sense at all only if there's no dispatching to an inexistent method, and your forbidding some methods would do the same, raise an exception.

Not in the case of subtypes. For example, the "class" Integer of which 
Positive is a "member" still has negative values, it is just so that you 
cannot tag them Positive. E.g.

    A : Positive := 1;
    B : Positive := 100;
    C : Positive := (A - B) + B; -- This is OK, though A - B = -99

> So to avoid that, if you want to disallow operations (without make a dispatching call to it raises an exception), you have to cut the tagged type from its root, by hiding the fact it has a parent type, so as to disinherit the parent's method.

No. You can convert subtype to its base in order to lift the constraint. 
There is nothing wrong with that.

> If you don't want that, (I suppose) one would have to implement a per-operation basis dispatching mechanism with a list of allowed types for each methods ?

Not necessarily. You simply override disallowed operations with bodies 
raising Constraint_Error. The same technique is used for multiple dispatch:

    type T is tagged ...
    procedure Multiple (X, Y : T); -- This is legal
    type S is new T with ...
    X : T'Class := T;
    Y : T'Class := S;
begin
    Multiple (X, Y); -- Run-time exception!

> Could you give an example of 10, the syntax you would like ?

    type Kind_Of is (Wake_Up, Notify, Warning, Error, Alarm);
    task type Monitor is
       entry Signal (Kind_Of) (No : Positive; Name : String);
    end Monitor;

Now if you wanted to constrain Signal to only Wake_Up..Notify, that 
would not go.

> ... I wanted to have an anonymous string subtype in a record.

Well, that is another issue. Anonymous types (some) are allowed for 
object declarations, but not as part of other types declarations. 
Sometimes it is quite annoying, e.g. when declaring large nested record 
types:

    type Foo is record
       A : record ... end record; -- Illegal
       B : array (...) of ...;    -- Illegal
    end record;

Sometimes one wants anonymous types of arguments:

    procedure Init
              (  Section_1 : record ... end record;
                 Section_2 : record ... end record;
              );

in order to group them. That is illegal too.

> Shouldn't have been terribly important. I made without, but had to add a discriminant Length, which is idiotic since the only use for my record type is to be allocated dynamically, and the string field would always be initialized. In that case I wouldn't risk anything by having
> type LINK is record
>    Next: access LINK;
>    TEXT:String;
> end record; being legal.

This is again a different issue. You need discriminant in order to be 
able to specify/propagate the constraint.

> ... since all use would be like this: some_access_to_link := new (Next_link, new STRING(TEXT_INPUT)), and TEXT_INPUT would carry its Length information with it and the object would always have a fixed length.

No, it can be allocated on the stack and be a component of another type. 
It is a very important mechanism as it allows to avoid implicit use of 
the heap.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


  reply	other threads:[~2018-02-27  9:18 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-26 16:26 [Newbie] doubly constrained array, dumb question Mehdi Saada
2018-02-26 17:02 ` J-P. Rosen
2018-02-26 21:40   ` Dmitry A. Kazakov
2018-02-26 23:26   ` Randy Brukardt
2018-02-27  9:01     ` Simon Wright
2018-02-27 22:11       ` Randy Brukardt
2018-02-26 20:52 ` Niklas Holsti
2018-02-27  1:29 ` Mehdi Saada
2018-02-27  9:18   ` Dmitry A. Kazakov [this message]
2018-02-27 11:43     ` Mehdi Saada
2018-02-27 14:19       ` Dmitry A. Kazakov
2018-02-27 17:08     ` G. B.
2018-02-27 17:37       ` Dmitry A. Kazakov
2018-02-27 14:34   ` Jere
2018-02-27 15:13     ` Dmitry A. Kazakov
2018-02-27  7:38 ` Jacob Sparre Andersen
replies disabled

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