From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: [Newbie] doubly constrained array, dumb question Date: Tue, 27 Feb 2018 10:18:10 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <62f83fe5-15d6-41cf-952f-bc3cb077d42f@googlegroups.com> <473f9b1a-6466-4745-9041-107f54062cf2@googlegroups.com> NNTP-Posting-Host: MyFhHs417jM9AgzRpXn7yg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 X-Notice: Filtered by postfilter v. 0.8.3 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:50678 Date: 2018-02-27T10:18:10+01:00 List-Id: 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