comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: formal array types and default values
Date: Wed, 3 Jan 2018 10:33:31 +0200
Date: 2018-01-03T10:33:31+02:00	[thread overview]
Message-ID: <fb3iqsFnjd7U1@mid.individual.net> (raw)
In-Reply-To: <p2h9fr$6ji$1@franka.jacob-sparre.dk>

On 18-01-03 02:52 , Randy Brukardt wrote:
> "Niklas Holsti" <niklas.holsti@tidorum.invalid> wrote in message
> news:faqhpnFljnfU1@mid.individual.net...
>> On 17-12-30 23:42 , Mehdi Saada wrote:
>>> I have several questions more or less related.

    [snip]

>>> It would be nice to be able  to set a default value to an array type
>>> in a generic package, equal to a generic object parameter.
>>
>> Agreed. Possibly this case was forgotten, or prevented by such a generic
>> object parameter not being a static value.
>
> Not really. All of these defaults for for a *type*, not a subtype, so there
> is only one per type. (There are thorny semantic problems with per-subtype
> defaults, we decided they were not worth the trouble.) Thus, it wouldn't
> make sense to specify it for generic formal parameters, as presumably the
> actual type already has such a default (and if it doesn't, you can't add one
> in the generic unit).

I think that Mehdi Saada wanted to be able to set a default value for 
the components of an ordinary (not formal generic) array type declared 
/in/ the generic package, using a formal generic object as the default 
value. For example:

    generic

       type T is private;

       Null_Value : in T;

    package Foo is

       type Vector is array (Positive range <>) of T
          with Default_Component_Value => Null_Value;

    end Foo;

Following the current rules, GNAT rejects this because Null_Value is not 
(always) a scalar.

If the generic formal type T is changed to "type T is range <>", GNAT 
accepts the generic package declaration, but accepts an instantiation 
only if Null_Value is given a static expression (this is GNAT GPL 2012 
on Mac).

The drawback of the current restrictions (when T is private) is that the 
responsibility for the assignment of a default value for the components 
of the Vector type is placed on the /client/ of the generic package, who 
must set the default value for the actual type T. Moreover, it is now 
impossible to have different default component values for different 
arrays declared in the generic, because type T can have only one default 
value.

One can imagine, for example, that T represents a value in some algebra 
that has both a "zero" value (neutral element for addition) and a "one" 
value (neutral element for multiplication), and that the generic package 
would like to use "zero" as the default component value for one array 
type ("array of sums") but use "one" as the default component value for 
another array type ("array of products").

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


  reply	other threads:[~2018-01-03  8:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-30 21:42 formal array types and default values Mehdi Saada
2017-12-30 22:20 ` Niklas Holsti
2017-12-30 23:59   ` Mehdi Saada
2018-01-03  0:52   ` Randy Brukardt
2018-01-03  8:33     ` Niklas Holsti [this message]
2018-01-03 20:53       ` Randy Brukardt
2018-01-06  0:10         ` Niklas Holsti
2018-01-08 21:12           ` Randy Brukardt
2017-12-31 12:34 ` Jeffrey R. Carter
2017-12-31 14:55   ` Mehdi Saada
2018-01-03 15:47 ` Mehdi Saada
2018-01-03 21:16   ` Randy Brukardt
2018-01-04 15:02 ` Mehdi Saada
replies disabled

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