Version 1.1 of ai12s/ai12-0087-1.txt

Unformatted version of ai12s/ai12-0087-1.txt version 1.1
Other versions for file ai12s/ai12-0087-1.txt

!standard 3.5(27)          13-10-31 AI12-0087-1/01
!class Amendment 13-10-31
!status work item 13-10-31
!status received 13-09-03
!priority Low
!difficulty Easy
!subject Reading the default value of a subtype
!summary
An attribute Default_Value is defined for scalar subtypes. It has the type of the subtype and returns the specified default value if any.
!problem
It is not possible to determine the default value of a type. This can be useful to determine whether values have been initialized, for instance.
!proposal
(See summary.)
!wording
** TBD. Probably add this after 3.5(27).
!discussion
It's a little odd that you can set this value but not read it. We didn't provide a way to read it because there is no sensible value to return if it is not set. It seems that there are two ways to handle the case of the value when the aspect is not set:
(1) Have the value returned by the attribute to be unspecified (but inside the base range of the type); or
(2) Make the aspect illegal if it is not specified. We'd need a runtime check similar to the one for 'First on a subtype with a predicate, so that we don't have a contract model violation for formal scalar types.
Note that the pragma Normalize_Scalars has a similar effect to specifying the aspect Default_Value. It would be useful if the attribute could return the actual default value in that case (but that is a nice-to-have as opposed to a requirement). [Aside: One wonders if Normalize_Scalars should be defined in terms of implicitly setting the aspect Default_Value to a value in the base range of the type but (hopefully) outside of the subtype constraint of the type. Allowing invalid values might be tricky, but useful even for explicit settings.]
This can be worked around with some planning. That is, ensure that the default value is set to a named constant and not just a value:
My_Default_Int_Value : constant := 0;
type My_Integer_Type is new Integer with Default_Value => My_Default_Int_Value;
!example
pragma Assert ((for all I in 1..10 => A(I) /= S'Default_Value));
!ASIS
** A new ASIS attribute kind is needed **
!ACATS test
An ACATS C-Test is needed (and a B-Test if there are any illegal cases).
!appendix

From: Jeff Cousins
Sent: Tuesday, September 3, 2013  5:06 AM

[Editor's note: This message was blank for some reason.]

****************************************************************

From: Randy Brukardt
Sent: Tuesday, September 3, 2013  12:18 PM

Jeff: This message had some error in it such that it was totally blank when I
got it. Perhaps you need to resend it a different way?

****************************************************************

From: Bob Duff
Sent: Wednesday, September 4, 2013  7:38 AM

And it corrupted my mailbox, after printing some message about a bad base64
digit.

****************************************************************

From: Tucker Taft
Sent: Tuesday, September 3, 2013  12:25 PM

> But how do I obtain the default value?  Do we need an attribute
S'Default_Value?

In general it would be easier for the user if an attribute were associated
with all aspects for which it makes sense.

> Alternatively I could declare a constant of the type and hope that it 
> picks up the default value, but that appears to be illegal without an 
> initialisation expression.  It looks odd, but is there any reason to 
> disallow constants without an initialisation expression as long as all 
> components have defaults?  (I now expect to be shouted at!)

I much prefer being explicit here, hence the attribute.  Also, this would
look like a deferred constant.

It would also be consistent to allow "<>" as the initial value, but we seem
to have considered that syntax and rejected it several times.

> type My_Integer_Type is new Integer
>    with Default_Value => 0;
>
> My_Integer : constant My_Integer_Type; -- Illegal

Actually, not illegal in the spec of a package.  It would be a deferred
constant in that context.

> ... but I feel that an attribute would be preferable.

Agreed.

****************************************************************

From: Jeff Cousins
Sent: Wednesday, September 4, 2013  8:32 AM

Mmmm...  Tucker seemed to have got it ok, I got it bounced back - twice in
fact, once shortly after I sent it then again about 11 hours later.

---

Hello.  I've been trying to write a program from scratch as if Ada 2012 had
been available from the outset, and first new feature that I've found to be
useful is default values for scalars. My little program sets up a table of
values, and at the end I wanted to do a check that all entries had been set
to something (i.e. didn't still contain the default value).

But how do I obtain the default value?  Do we need an attribute
S'Default_Value?

Alternatively I could declare a constant of the type and hope that it picks
up the default value, but that appears to be illegal without an initialisation
expression.  It looks odd, but is there any reason to disallow constants
without an initialisation expression as long as all components have defaults?
(I now expect to be shouted at!)

type My_Integer_Type is new Integer
  with Default_Value => 0;

My_Integer : constant My_Integer_Type; -- Illegal


The easy work around is

My_Default_Int_Value : constant := 0;

type My_Integer_Type is new Integer
  with Default_Value => My_Default_Int_Value;

but I feel that an attribute would be preferable.

****************************************************************

Questions? Ask the ACAA Technical Agent