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!feeder.eternal-september.org!aioe.org!.POSTED.3d73Ybk3C5U4I2t8lv+lAQ.user.gioia.aioe.org!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: gnat.string_split , howto manipulate slice numbers Date: Thu, 31 Oct 2019 11:12:39 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <5d710dac-d172-4a31-899e-1bf95e220ed6@googlegroups.com> <7fb6d1d0-18df-43b9-bcdc-9782dfb5171c@googlegroups.com> NNTP-Posting-Host: 3d73Ybk3C5U4I2t8lv+lAQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:57397 Date: 2019-10-31T11:12:39+01:00 List-Id: On 2019-10-30 12:30, Simon Wright wrote: > "Dmitry A. Kazakov" writes: > >> On 2019-10-29 19:33, J-P. Rosen wrote: >>> Le 29/10/2019 à 17:47, Simon Wright a écrit : >>>> type Slice_Number is new Natural; >>>> >>>> For me, this carries declaring a type rather than a subtype further than >>>> necessary. A subtype? Or why not just use Natural? >>> Integer (or its subtypes) should be avoided in general. It is non >>> portable, and carries no information to the reader about the purpose of >>> the type. Making appropriate types that cannot be mixed is key to the >>> philosophy of Ada ("strong typing", you know...). >> >> Another reason is to distinguish index/key from position. When both >> are subtypes of integer some bugs may slip through, e.g. >> >> A (A'Length - 1) >> >> This is broken code, but the compiler cannot detect it. > > How is it broken? It should have been A (A'Last - 1) A'Length is position/offset, only occasionally numerically equal to index. Compare it with Time and Duration. These are two distinct types. Array index is like Time. Array length is like Duration, a difference between two Times. BTW, here come further issues why indexing by integer types is not so good idea from the strong typing point of view. Arithmetic on indices does not make sense, like adding or multiplying times does not. Except some rare cases of low-level programming index arithmetic is an error. If Ada had a more powerful type system one would not only declare a new index type as J-P suggested, but also make sure the arithmetic would be replaced with this: function "-" (Left, Right : Index_Type) return Universal_Integer; function "-" (Left : Index_Type; Right : Universal_Integer) return Index_Type; function "-" (Left : Universal_Integer; Right : Index_Type) return Index_Type; function "+" (Left : Universal_Integer; Right : Index_Type) return Index_Type; function "+" (Left : Index_Type; Right : Universal_Integer) return Index_Type; >> Or when you instantiate a generic container: >> >> generic >> type Position_Type is range <>; >> type Index_Type is private; >> type Element_Type is private; >> package Generic_Container is >> ... >> function Get (Container : Container_Type; Position : Position_Type) >> return Element_Type; >> function Get (Container : Container_Type; Key : Key_Type) >> return Element_Type; >> >> It fails when instantiated with subtypes of same integer type. > > Again.You'd need to use named parameter association, but otherwise, what > am I missing? The problem is with Get. When you instantiate Generic_Container like: package Oops is new Generic_Container (Positive, Integer, Float); Then two declarations of Get will collide. [Ada generics are only weakly typed] -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de