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

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

!standard 3.5(15)          15-10-08 AI12-0087-1/02
!standard 3.5(55.4/4)
!standard 3.6.2(10)
!class Amendment 13-10-31
!status No Action (7-0-1) 15-10-17
!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
Add after 3.5(15) [Author's note: This is before the first of the function attributes][Editor's note: I thought that the appropriate place was just before the image attribute, after all of the important ones (3.5(27)); not a big deal either way]:
S'Default_Value
S'Default_Value denotes the value of the Default_Value aspect of the type of S.
S'Has_Default_Value
If the type of S has the Default_Value aspect defined, then S'Has_Default_Value returns True; otherwise, it returns False.
Add after 3.5(55.4/4)):
Legality Rules
It is illegal to evaluate S'Default_Value when the type of S does not have the Default_Value aspect defined. If this Legality Rule is violated in an instance of a generic unit then Program_Error is raised at the point of the violation.
[Editor's note: I'd borrow the wording from 3.2.4(26/3) and 3.2.4(29.1/4) for the above, as that is the closest analog in the Standard. The second sentence is close (but it should be under Dynamic Semantics); the first is very different for no obvious reason.]
Add after 3.6.2(10):
A'Default_Component_Value
For an array with a scalar component type, A'Default_Component_Value denotes the value of the Default_Component_Value aspect of A.
A'Has_Default_Component_Value
For an array with a scalar component type, if A has the Default_Component_Value aspect defined, then A'Has_Default_Component_Value returns True; otherwise, it returns False.
Legality Rules
It is illegal to evaluate A'Default_Component_Value when A does not have the Default_Component_Value aspect defined. If this Legality Rule is violated in an instance of a generic unit then Program_Error is raised at the point of the violation.
!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.

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

From: Jean-Pierre Rosen
Sent: Tuesday, September 8, 2015  12:09 AM

[Replying to version /02 of this AI - Editor.]

> Add to the list of attributes in 3.5 (I would suggest before the first 
> of the function attributes, i.e. after (15)):
> 
> S'Default_Value S'Default_Value denotes the value of the Default_Value 
> aspect of the type of S.
> 
> S'Has_Default_Value If the type of S has the Default_Value aspect 
> defined, then S'Has_Default_Value returns True; otherwise, it returns False.
 
This might have been discussed before, but what about applying these attributes
to record components?

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

From: Jeff Cousins
Sent: Wednesday, September 9, 2015  8:38 AM

Default_Component_Value was added at Madrid.  Presumably record components
would similarly be limited to components of a scalar type.
We'd have to invent some syntax such as:

S.<component_name>'Default_Value

But then we ought to have corresponding syntax for setting the default value
(in addition to the current method), such as:

with <component_name>.Default_Value => ...
or
with Default_Value (<component1_name> => ..., <component2_name> => ..., ...) (I
quite like that second alternative).

I think this would be venturing into a new AI.

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

From: Jeff Cousins
Sent: Thursday, September 22, 2015  7:31 AM

I don't think we say for 'First or 'Last that the attribute cannot be set by
the user (as an attribute, of course the user sets it when he defines the
range of whatever it refers to).

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

Questions? Ask the ACAA Technical Agent