comp.lang.ada
 help / color / mirror / Atom feed
* Possible to recover default value of scalar type?
@ 2020-12-13  9:54 reinert
  2020-12-14  0:08 ` Stephen Leake
  0 siblings, 1 reply; 23+ messages in thread
From: reinert @ 2020-12-13  9:54 UTC (permalink / raw)


Assume the following code:

type A_Type is new Natural range 0..9 with Default_Value => 9;
A : A_Type;

Is it later on here possible to get access to the default value (9) ?
If A was a component of a record, one could get it "9" via

  some_record'(others =><>).A

But more directly?

reinert

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-13  9:54 Possible to recover default value of scalar type? reinert
@ 2020-12-14  0:08 ` Stephen Leake
  2020-12-14  6:09   ` reinert
  0 siblings, 1 reply; 23+ messages in thread
From: Stephen Leake @ 2020-12-14  0:08 UTC (permalink / raw)


reinert <reinkor@gmail.com> writes:

> Assume the following code:
>
> type A_Type is new Natural range 0..9 with Default_Value => 9;
> A : A_Type;
>
> Is it later on here possible to get access to the default value (9) ?
> If A was a component of a record, one could get it "9" via
>
>   some_record'(others =><>).A
>
> But more directly?

You were almost there:

procedure Debug
is
   type A_Type is new Natural range 0 .. 9 with Default_Value => 9;
   A : A_Type;
begin
   Put_Line ("a_type default" & A'Image);
end Debug;

This outputs:

a_type default 9

An uninitialized variable of the type has the specified default value;
that's the whole point.

-- 
-- Stephe

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14  0:08 ` Stephen Leake
@ 2020-12-14  6:09   ` reinert
  2020-12-14  9:01     ` AdaMagica
  0 siblings, 1 reply; 23+ messages in thread
From: reinert @ 2020-12-14  6:09 UTC (permalink / raw)


Yes, got this. However, in my example when the scalar is a component in a record,
I did not need to define a new variable. But it seems I need when the scalar is "alone"?

reinert

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14  6:09   ` reinert
@ 2020-12-14  9:01     ` AdaMagica
  2020-12-14  9:38       ` Dmitry A. Kazakov
  2020-12-15  1:21       ` Randy Brukardt
  0 siblings, 2 replies; 23+ messages in thread
From: AdaMagica @ 2020-12-14  9:01 UTC (permalink / raw)


I do not really understand the problem. It seems you want to be able to access the default value like so:
N: Natural := Natural(A_Type'Default_Value);
This is not possible. Thre is no corresponding attribute 'Default_Value.
If this presents a real problem, submit it to Ada comment stating why this is important.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14  9:01     ` AdaMagica
@ 2020-12-14  9:38       ` Dmitry A. Kazakov
  2020-12-14 15:56         ` AdaMagica
  2020-12-15  1:26         ` Randy Brukardt
  2020-12-15  1:21       ` Randy Brukardt
  1 sibling, 2 replies; 23+ messages in thread
From: Dmitry A. Kazakov @ 2020-12-14  9:38 UTC (permalink / raw)


On 2020-12-14 10:01, AdaMagica wrote:
> I do not really understand the problem. It seems you want to be able to access the default value like so:
> N: Natural := Natural(A_Type'Default_Value);
> This is not possible. Thre is no corresponding attribute 'Default_Value.
> If this presents a real problem, submit it to Ada comment stating why this is important.

It could in the cases like this:

    procedure Library_Foo (Bar : Baz := Baz'Default_Value)

You can declare constants in some places, but not at the library level. 
But in any case, being forced to declare a constant each time you need 
to get at the default value?

The same problem arises with container generics. If you have an array 
keeping container elements, logically freed elements need to be 
"destroyed" in some way. The default type value would be that thing as 
well as a default for Null_Element, if used.

I think that all non-limited types one could declare uninitialized, must 
have S'Default_Value equal to the default value the compiler would use. 
And it should produce same warnings uninitialized values do:

    Put_Line (String (1..10)'Default_Value); -- print garbage

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14  9:38       ` Dmitry A. Kazakov
@ 2020-12-14 15:56         ` AdaMagica
  2020-12-14 16:31           ` Dmitry A. Kazakov
  2020-12-15  1:27           ` Randy Brukardt
  2020-12-15  1:26         ` Randy Brukardt
  1 sibling, 2 replies; 23+ messages in thread
From: AdaMagica @ 2020-12-14 15:56 UTC (permalink / raw)


Dmitry A. Kazakov schrieb am Montag, 14. Dezember 2020 um 10:38:42 UTC+1:
> procedure Library_Foo (Bar : Baz := Baz'Default_Value) 
Suppose type Baz has no default value aspect. Then a call Library_Foo without parameter would use what?
A solution could be that the attribute is illegal if there is no aspect. The compiler knows.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14 15:56         ` AdaMagica
@ 2020-12-14 16:31           ` Dmitry A. Kazakov
  2020-12-14 18:24             ` Simon Wright
  2020-12-15  1:27           ` Randy Brukardt
  1 sibling, 1 reply; 23+ messages in thread
From: Dmitry A. Kazakov @ 2020-12-14 16:31 UTC (permalink / raw)


On 2020-12-14 16:56, AdaMagica wrote:
> Dmitry A. Kazakov schrieb am Montag, 14. Dezember 2020 um 10:38:42 UTC+1:
>> procedure Library_Foo (Bar : Baz := Baz'Default_Value)
> Suppose type Baz has no default value aspect. Then a call Library_Foo without parameter would use what?

The default used by the compiler in this:

    declare
       Bar : Baz;
    begin

with an appropriate warning of course.

[ It was a language design bug to allow implicitly uninitialized 
variables in the first place. Declarations like above should have been 
illegal. ]

> A solution could be that the attribute is illegal if there is no aspect. The compiler knows.

I would argue that if

    declare
       Bar : Baz;
    begin

is legal, then it must be logically equivalent to:

    declare
       Bar : Baz := Baz'Default_Value;
    begin

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14 16:31           ` Dmitry A. Kazakov
@ 2020-12-14 18:24             ` Simon Wright
  2020-12-14 18:53               ` Dmitry A. Kazakov
  0 siblings, 1 reply; 23+ messages in thread
From: Simon Wright @ 2020-12-14 18:24 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> [ It was a language design bug to allow implicitly uninitialized
> variables in the first place. Declarations like above should have been
> illegal. ]

There is an argument that you should only initialise variables at the
point of declaration if you know what value they should take; so that
the compiler can detect the use of uninitialised variables.

If you always initialize variables, even if you don't know what value
they should take, the compiler can't help you if you forget to assign
the correct value.

Personally I always try hard not to declare an uninitialised variable.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14 18:24             ` Simon Wright
@ 2020-12-14 18:53               ` Dmitry A. Kazakov
  2020-12-15  6:47                 ` J-P. Rosen
  0 siblings, 1 reply; 23+ messages in thread
From: Dmitry A. Kazakov @ 2020-12-14 18:53 UTC (permalink / raw)


On 2020-12-14 19:24, Simon Wright wrote:
> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
> 
>> [ It was a language design bug to allow implicitly uninitialized
>> variables in the first place. Declarations like above should have been
>> illegal. ]
> 
> There is an argument that you should only initialise variables at the
> point of declaration if you know what value they should take; so that
> the compiler can detect the use of uninitialised variables.

I think Robert Dewar argued that variables must be declared in the 
narrowest possible scope. Which would imply that at the beginning of 
that scope you should know the value, because it would be the first use 
of the variable.

> If you always initialize variables, even if you don't know what value
> they should take, the compiler can't help you if you forget to assign
> the correct value.

> Personally I always try hard not to declare an uninitialised variable.

Same here.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14  9:01     ` AdaMagica
  2020-12-14  9:38       ` Dmitry A. Kazakov
@ 2020-12-15  1:21       ` Randy Brukardt
  1 sibling, 0 replies; 23+ messages in thread
From: Randy Brukardt @ 2020-12-15  1:21 UTC (permalink / raw)


"AdaMagica" <christ-usch.grein@t-online.de> wrote in message 
news:82e629ea-bd59-417a-9185-dd6094e396c1n@googlegroups.com...
>I do not really understand the problem. It seems you want to be able to 
>access the default value like so:
> N: Natural := Natural(A_Type'Default_Value);
> This is not possible. Thre is no corresponding attribute 'Default_Value.
> If this presents a real problem, submit it to Ada comment stating why this 
> is important.

We considered an attribute like that, but it becomes a semantic problem if 
the type doesn't have a Default_Value and you are in a context where you 
don't know (such as for a generic formal type). I vaguely remember some 
other semantic problem, but I don't remember the details. These things could 
be worked out, but it seemed messy.

I've long wanted <> to work as it does in aggregates generally (if that 
existed, I'd also have a restriction to require all objects to be 
initialized; that would provide an encouragement to initialize as many 
objects as possible; right now, the iffy thing (not initializing) is the 
easiest).

                              Randy.


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14  9:38       ` Dmitry A. Kazakov
  2020-12-14 15:56         ` AdaMagica
@ 2020-12-15  1:26         ` Randy Brukardt
  2020-12-15  7:35           ` Dmitry A. Kazakov
  1 sibling, 1 reply; 23+ messages in thread
From: Randy Brukardt @ 2020-12-15  1:26 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:rr7bqv$19da$1@gioia.aioe.org...
> On 2020-12-14 10:01, AdaMagica wrote:
>> I do not really understand the problem. It seems you want to be able to 
>> access the default value like so:
>> N: Natural := Natural(A_Type'Default_Value);
>> This is not possible. Thre is no corresponding attribute 'Default_Value.
>> If this presents a real problem, submit it to Ada comment stating why 
>> this is important.
>
> It could in the cases like this:
>
>    procedure Library_Foo (Bar : Baz := Baz'Default_Value)

I would have suggested to write this as:
     procedure Library_Foo (Bar : Baz := <>)
since this is the syntax used in aggregates (and why should aggregates have 
all the fun??).

>    Put_Line (String (1..10)'Default_Value); -- print garbage

The above isn't a legal attribute prefix in any case (can't slice a type). 
And you don't need to because this is clearly an aggregate (which is legal 
in Ada 2012):
    Put_Line (String'(1..10 => <>)); -- print garbage

                                Randy.


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14 15:56         ` AdaMagica
  2020-12-14 16:31           ` Dmitry A. Kazakov
@ 2020-12-15  1:27           ` Randy Brukardt
  2020-12-15  9:30             ` Niklas Holsti
  1 sibling, 1 reply; 23+ messages in thread
From: Randy Brukardt @ 2020-12-15  1:27 UTC (permalink / raw)


"AdaMagica" <christ-usch.grein@t-online.de> wrote in message 
news:570e9d30-0b33-45f0-a9fe-163cc810a770n@googlegroups.com...
> Dmitry A. Kazakov schrieb am Montag, 14. Dezember 2020 um 10:38:42 UTC+1:
>> procedure Library_Foo (Bar : Baz := Baz'Default_Value)
> Suppose type Baz has no default value aspect. Then a call Library_Foo 
> without parameter would use what?
> A solution could be that the attribute is illegal if there is no aspect. 
> The compiler knows.

Not always. Never forget generics. One would hope to be able to use this on 
generic formal types, as most of them are going to have default values (at 
least in new code).

                        Randy.


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-14 18:53               ` Dmitry A. Kazakov
@ 2020-12-15  6:47                 ` J-P. Rosen
  2020-12-15  7:23                   ` Dmitry A. Kazakov
  2020-12-17  0:48                   ` Randy Brukardt
  0 siblings, 2 replies; 23+ messages in thread
From: J-P. Rosen @ 2020-12-15  6:47 UTC (permalink / raw)


Le 14/12/2020 à 19:53, Dmitry A. Kazakov a écrit :
> I think Robert Dewar argued that variables must be declared in the
> narrowest possible scope. Which would imply that at the beginning of
> that scope you should know the value, because it would be the first use
> of the variable.

Not applicable if your variable is used in a loop:

   V : Integer;
begin
   loop
      Get (V);
      exit when V =0;
      -- do something with V
   end loop;

Clearly, initializing V makes no sense.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15  6:47                 ` J-P. Rosen
@ 2020-12-15  7:23                   ` Dmitry A. Kazakov
  2020-12-15  9:07                     ` J-P. Rosen
  2020-12-17  0:48                   ` Randy Brukardt
  1 sibling, 1 reply; 23+ messages in thread
From: Dmitry A. Kazakov @ 2020-12-15  7:23 UTC (permalink / raw)


On 2020-12-15 07:47, J-P. Rosen wrote:
> Le 14/12/2020 à 19:53, Dmitry A. Kazakov a écrit :
>> I think Robert Dewar argued that variables must be declared in the
>> narrowest possible scope. Which would imply that at the beginning of
>> that scope you should know the value, because it would be the first use
>> of the variable.
> 
> Not applicable if your variable is used in a loop:
> 
>     V : Integer;
> begin
>     loop
>        Get (V);
>        exit when V =0;
>        -- do something with V
>     end loop;
> 
> Clearly, initializing V makes no sense.

    loop
       declare
          V : constant Integer := Get;
       begin
          exit when V = 0;
          -- do something with V
       end;
    end loop;

It is related to another long standing issue with returning values 
(multiple values) from functions and functions with in out parameters 
(resolved recently).

Returning to Simon's argument about detecting uninitialized variables. 
It would not apply to your example.

    V : Integer := <>;  -- Invented syntax for explicit lack of
                        -- initialization
begin
    loop
       Get (V);
       exit when V = 0;
       -- do something with V
    end loop;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15  1:26         ` Randy Brukardt
@ 2020-12-15  7:35           ` Dmitry A. Kazakov
  2020-12-17  0:43             ` Randy Brukardt
  0 siblings, 1 reply; 23+ messages in thread
From: Dmitry A. Kazakov @ 2020-12-15  7:35 UTC (permalink / raw)


On 2020-12-15 02:26, Randy Brukardt wrote:
> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message

>>     Put_Line (String (1..10)'Default_Value); -- print garbage
> 
> The above isn't a legal attribute prefix in any case (can't slice a type).

I mean a subtype.

> And you don't need to because this is clearly an aggregate (which is legal
> in Ada 2012):
>      Put_Line (String'(1..10 => <>)); -- print garbage

Yes, I would prefer the box notation too. However having a proper name 
would has some advantages too:

    subtype S is T range T'Default_Value - 100..T'Default_Value + 100;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15  7:23                   ` Dmitry A. Kazakov
@ 2020-12-15  9:07                     ` J-P. Rosen
  2020-12-15 16:53                       ` Simon Wright
  0 siblings, 1 reply; 23+ messages in thread
From: J-P. Rosen @ 2020-12-15  9:07 UTC (permalink / raw)


Le 15/12/2020 à 08:23, Dmitry A. Kazakov a écrit :
>> Not applicable if your variable is used in a loop:
>>
>>     V : Integer;
>> begin
>>     loop
>>        Get (V);
>>        exit when V =0;
>>        -- do something with V
>>     end loop;
>>
>> Clearly, initializing V makes no sense.
> 
>    loop
>       declare
>          V : constant Integer := Get;
>       begin
>          exit when V = 0;
>          -- do something with V
>       end;
>    end loop;
> 
Well, you can push anything in a function, but it's not always
clear/readable/simpler...

> It is related to another long standing issue with returning values
> (multiple values) from functions and functions with in out parameters
> (resolved recently).
> 
> Returning to Simon's argument about detecting uninitialized variables.
> It would not apply to your example.
> 
>    V : Integer := <>;  -- Invented syntax for explicit lack of
>                        -- initialization
> begin
>    loop
>       Get (V);
>       exit when V = 0;
>       -- do something with V
>    end loop;
That would make more sense: make initialization required, and say so if
you don't care.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15  1:27           ` Randy Brukardt
@ 2020-12-15  9:30             ` Niklas Holsti
  2020-12-17  0:45               ` Randy Brukardt
  0 siblings, 1 reply; 23+ messages in thread
From: Niklas Holsti @ 2020-12-15  9:30 UTC (permalink / raw)


On 2020-12-15 3:27, Randy Brukardt wrote:
> "AdaMagica" <christ-usch.grein@t-online.de> wrote in message
> news:570e9d30-0b33-45f0-a9fe-163cc810a770n@googlegroups.com...
>> Dmitry A. Kazakov schrieb am Montag, 14. Dezember 2020 um 10:38:42 UTC+1:
>>> procedure Library_Foo (Bar : Baz := Baz'Default_Value)
>> Suppose type Baz has no default value aspect. Then a call Library_Foo
>> without parameter would use what?
>> A solution could be that the attribute is illegal if there is no aspect.
>> The compiler knows.
> 
> Not always. Never forget generics. One would hope to be able to use this on
> generic formal types, as most of them are going to have default values (at
> least in new code).


The generic formal type declarations could be extended to indicate that 
a Default_Value is required, for example:

    generic
       type T is private with Default_Value;

    ...





^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15  9:07                     ` J-P. Rosen
@ 2020-12-15 16:53                       ` Simon Wright
  2020-12-15 18:14                         ` AdaMagica
  0 siblings, 1 reply; 23+ messages in thread
From: Simon Wright @ 2020-12-15 16:53 UTC (permalink / raw)


"J-P. Rosen" <rosen@adalog.fr> writes:

> Le 15/12/2020 à 08:23, Dmitry A. Kazakov a écrit :

>> Returning to Simon's argument about detecting uninitialized variables.
>> It would not apply to your example.
>> 
>>    V : Integer := <>;  -- Invented syntax for explicit lack of
>>                        -- initialization
>> begin
>>    loop
>>       Get (V);
>>       exit when V = 0;
>>       -- do something with V
>>    end loop;

> That would make more sense: make initialization required, and say so
> if you don't care.

I suppose the compiler could still detect & warn about inadvertent use
of the don't-care value.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15 16:53                       ` Simon Wright
@ 2020-12-15 18:14                         ` AdaMagica
  2020-12-17  0:53                           ` Randy Brukardt
  0 siblings, 1 reply; 23+ messages in thread
From: AdaMagica @ 2020-12-15 18:14 UTC (permalink / raw)


Simon Wright schrieb am Dienstag, 15. Dezember 2020 um 17:54:01 UTC+1:
> I suppose the compiler could still detect & warn about inadvertent use 
> of the don't-care value.
Just a story about my work (long ago):
Our coding standard required for every type declaration a default value that indicated an uninitialised value:

type T is ...
Nd_T: constant T := ...;  -- Nd: not defined
X: T := Nd_T;  -- required

The idea was that this Nd value should be thus that it would be likely to produce an exception when used in an expression. Also any change of this value should have absolutely no effect on the code.
In any case, at some time it was decided that the Nd value for numeric types was 0. The effect:
It was no longer possible to see whether in a declaration like
X: T := Nd_T;
this values denoted a truly undefined value or a concrete and correct initial value.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15  7:35           ` Dmitry A. Kazakov
@ 2020-12-17  0:43             ` Randy Brukardt
  0 siblings, 0 replies; 23+ messages in thread
From: Randy Brukardt @ 2020-12-17  0:43 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:rr9p04$17qd$1@gioia.aioe.org...
> On 2020-12-15 02:26, Randy Brukardt wrote:
>> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message
>
>>>     Put_Line (String (1..10)'Default_Value); -- print garbage
>>
>> The above isn't a legal attribute prefix in any case (can't slice a 
>> type).
>
> I mean a subtype.
>
>> And you don't need to because this is clearly an aggregate (which is 
>> legal
>> in Ada 2012):
>>      Put_Line (String'(1..10 => <>)); -- print garbage
>
> Yes, I would prefer the box notation too. However having a proper name 
> would has some advantages too:
>
>    subtype S is T range T'Default_Value - 100..T'Default_Value + 100;

If box was generally allowed, you could qualify it to get this effect:

    subtype S is T range T'(<>) - 100 .. T'(<>) + 100; -- Not Ada, but 
should be IMHO. :-)

and it's shorter, too. Of course, if T doesn't have a default value, neither 
of the above is a good idea. :-)

                         Randy.


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15  9:30             ` Niklas Holsti
@ 2020-12-17  0:45               ` Randy Brukardt
  0 siblings, 0 replies; 23+ messages in thread
From: Randy Brukardt @ 2020-12-17  0:45 UTC (permalink / raw)


"Niklas Holsti" <niklas.holsti@tidorum.invalid> wrote in message 
news:i3re1oF5omrU1@mid.individual.net...
> On 2020-12-15 3:27, Randy Brukardt wrote:
>> "AdaMagica" <christ-usch.grein@t-online.de> wrote in message
>> news:570e9d30-0b33-45f0-a9fe-163cc810a770n@googlegroups.com...
>>> Dmitry A. Kazakov schrieb am Montag, 14. Dezember 2020 um 10:38:42 
>>> UTC+1:
>>>> procedure Library_Foo (Bar : Baz := Baz'Default_Value)
>>> Suppose type Baz has no default value aspect. Then a call Library_Foo
>>> without parameter would use what?
>>> A solution could be that the attribute is illegal if there is no aspect.
>>> The compiler knows.
>>
>> Not always. Never forget generics. One would hope to be able to use this 
>> on
>> generic formal types, as most of them are going to have default values 
>> (at
>> least in new code).
>
>
> The generic formal type declarations could be extended to indicate that a 
> Default_Value is required, for example:
>
>    generic
>       type T is private with Default_Value;

Sure, but more complication. We (as a group) decided it wasn't worth it. I'd 
rather extend <> anyway, and that would be allowed anywhere, much like a 
raise_expression.

                    Randy.


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15  6:47                 ` J-P. Rosen
  2020-12-15  7:23                   ` Dmitry A. Kazakov
@ 2020-12-17  0:48                   ` Randy Brukardt
  1 sibling, 0 replies; 23+ messages in thread
From: Randy Brukardt @ 2020-12-17  0:48 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1117 bytes --]

"J-P. Rosen" <rosen@adalog.fr> wrote in message 
news:rr9m63$ufd$1@dont-email.me...
> Le 14/12/2020 à 19:53, Dmitry A. Kazakov a écrit :
>> I think Robert Dewar argued that variables must be declared in the
>> narrowest possible scope. Which would imply that at the beginning of
>> that scope you should know the value, because it would be the first use
>> of the variable.
>
> Not applicable if your variable is used in a loop:
>
>   V : Integer;
> begin
>   loop
>      Get (V);
>      exit when V =0;
>      -- do something with V
>   end loop;
>
> Clearly, initializing V makes no sense.

Saying that you *meant* to have an uninitialized value does make sense, 
though:

     V : Integer := <>; -- Not Ada, but should be IMHO.

Whenever something is omitted, one never knows whether it was on purpose or 
a mistake. You get similar issues when "else" is omitted (RR's style guide 
only allows that in very specific circumstances). It's unfortunate that Ada 
doesn't have a positive way to indicate default initialization, outside of 
aggregates.

                        Randy. 


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Possible to recover default value of scalar type?
  2020-12-15 18:14                         ` AdaMagica
@ 2020-12-17  0:53                           ` Randy Brukardt
  0 siblings, 0 replies; 23+ messages in thread
From: Randy Brukardt @ 2020-12-17  0:53 UTC (permalink / raw)


"AdaMagica" <christ-usch.grein@t-online.de> wrote in message 
news:a75e5fc2-8393-4c2b-85b9-7c3b4cfef85dn@googlegroups.com...
> Simon Wright schrieb am Dienstag, 15. Dezember 2020 um 17:54:01 UTC+1:
>> I suppose the compiler could still detect & warn about inadvertent use
>> of the don't-care value.
> Just a story about my work (long ago):
> Our coding standard required for every type declaration a default value 
> that indicated an uninitialised value:
>
> type T is ...
> Nd_T: constant T := ...;  -- Nd: not defined
> X: T := Nd_T;  -- required
>
> The idea was that this Nd value should be thus that it would be likely to 
> produce an exception when used in an expression. Also any change of this 
> value should have absolutely no effect on the code.
> In any case, at some time it was decided that the Nd value for numeric 
> types was 0. The effect:
> It was no longer possible to see whether in a declaration like
> X: T := Nd_T;
> this values denoted a truly undefined value or a concrete and correct 
> initial value.

Typically, values like this, at least those used in debuggers, use some 
premutation of 16#DEADBEEF# since it it obvious in data dumps, and is a 
rather unlikely value to be intended. The next version of Janus/Ada will 
initialize all "uninitialized" objects to this value unless you tell it not 
to. (Essentially, a version of Normalize_Scalars, except that these days it 
doesn't make much sense for that not to be the default. Optimization can 
remove most of unneeded initializations, and if they are actually needed, 
it's better to have a known dubious value than stack garbage.)

                               Randy. 


^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2020-12-17  0:53 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-13  9:54 Possible to recover default value of scalar type? reinert
2020-12-14  0:08 ` Stephen Leake
2020-12-14  6:09   ` reinert
2020-12-14  9:01     ` AdaMagica
2020-12-14  9:38       ` Dmitry A. Kazakov
2020-12-14 15:56         ` AdaMagica
2020-12-14 16:31           ` Dmitry A. Kazakov
2020-12-14 18:24             ` Simon Wright
2020-12-14 18:53               ` Dmitry A. Kazakov
2020-12-15  6:47                 ` J-P. Rosen
2020-12-15  7:23                   ` Dmitry A. Kazakov
2020-12-15  9:07                     ` J-P. Rosen
2020-12-15 16:53                       ` Simon Wright
2020-12-15 18:14                         ` AdaMagica
2020-12-17  0:53                           ` Randy Brukardt
2020-12-17  0:48                   ` Randy Brukardt
2020-12-15  1:27           ` Randy Brukardt
2020-12-15  9:30             ` Niklas Holsti
2020-12-17  0:45               ` Randy Brukardt
2020-12-15  1:26         ` Randy Brukardt
2020-12-15  7:35           ` Dmitry A. Kazakov
2020-12-17  0:43             ` Randy Brukardt
2020-12-15  1:21       ` Randy Brukardt

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