comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: gnat.string_split , howto manipulate slice numbers
Date: Thu, 31 Oct 2019 11:12:39 +0100
Date: 2019-10-31T11:12:39+01:00	[thread overview]
Message-ID: <qpec2q$12g5$1@gioia.aioe.org> (raw)
In-Reply-To: lyimo6xyfk.fsf@pushface.org

On 2019-10-30 12:30, Simon Wright wrote:
> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> 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

  parent reply	other threads:[~2019-10-31 10:12 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-29  1:54 gnat.string_split , howto manipulate slice numbers Alain De Vos
2019-10-29  8:17 ` briot.emmanuel
2019-10-29 16:56   ` Jeffrey R. Carter
2019-10-29 19:36     ` Niklas Holsti
2019-10-29 23:06       ` J-P. Rosen
2019-11-01  9:45         ` joakimds
2019-10-29 23:36   ` Stephen Leake
2019-10-29  8:35 ` Simon Wright
2019-10-29 11:08   ` Alain De Vos
2019-10-29 11:20     ` Alain De Vos
2019-10-29 16:44       ` J-P. Rosen
2019-10-29 16:47       ` Simon Wright
2019-10-29 18:33         ` J-P. Rosen
2019-10-29 19:37           ` Dmitry A. Kazakov
2019-10-30 11:30             ` Simon Wright
2019-10-31  9:30               ` joakimds
2019-10-31 10:12               ` Dmitry A. Kazakov [this message]
2019-10-31 18:36                 ` J-P. Rosen
2019-11-01  9:39                   ` joakimds
2019-10-29 16:41   ` Simon Wright
2019-10-29 16:53 ` Jeffrey R. Carter
replies disabled

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