comp.lang.ada
 help / color / mirror / Atom feed
* Why constant components are not permitted ?
@ 2011-11-24 18:14 David Sauvage
  2011-11-24 19:06 ` anon
                   ` (4 more replies)
  0 siblings, 5 replies; 27+ messages in thread
From: David Sauvage @ 2011-11-24 18:14 UTC (permalink / raw)


Hello,

given the following :

type Object is record
   Tag : constant String := "key"; -- GNAT compilation failed :
constant components are not permitted
end record;

In my case I find it useful to declare my Object Tag component as a
constant, but it is not permitted, I would be interested to learn
why ?


Cheers,




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

* Re: Why constant components are not permitted ?
  2011-11-24 18:14 Why constant components are not permitted ? David Sauvage
@ 2011-11-24 19:06 ` anon
  2011-11-24 19:49   ` David Sauvage
  2011-11-24 19:46 ` Ludovic Brenta
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 27+ messages in thread
From: anon @ 2011-11-24 19:06 UTC (permalink / raw)



  type test is record
                 a : integer := 16#FFFF# ;
                 b : string ( 1 .. 4 ) := "this" ; -- is OK
                 c : string := "is" ; -- error because no define boundary
               end record ;

Reason no one ask the ARG.


In <1856c00b-1994-406a-bbb3-73d93785099a@i6g2000vbe.googlegroups.com>, David Sauvage <david.sauvage@adalabs.com> writes:
>Hello,
>
>given the following :
>
>type Object is record
>   Tag : constant String := "key"; -- GNAT compilation failed :
>constant components are not permitted
>end record;
>
>In my case I find it useful to declare my Object Tag component as a
>constant, but it is not permitted, I would be interested to learn
>why ?
>
>
>Cheers,
>




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

* Re: Why constant components are not permitted ?
  2011-11-24 18:14 Why constant components are not permitted ? David Sauvage
  2011-11-24 19:06 ` anon
@ 2011-11-24 19:46 ` Ludovic Brenta
  2011-11-25  9:10   ` Dmitry A. Kazakov
  2011-11-25  6:56 ` Niklas Holsti
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 27+ messages in thread
From: Ludovic Brenta @ 2011-11-24 19:46 UTC (permalink / raw)


David Sauvage writes on comp.lang.ada:
> Hello,
>
> given the following :
>
> type Object is record
>    Tag : constant String := "key"; -- GNAT compilation failed : constant components are not permitted
> end record;
>
> In my case I find it useful to declare my Object Tag component as a
> constant, but it is not permitted, I would be interested to learn why?

Maybe because you don't need constant components.

If the purpose of a constant component is to identify the non-tagged
type of an object, then both the programmer and the compiler know the
type at compile time.

If the purpose is to identify the tagged type of an object, then the
compiler emits a tag as part of the representation of each object for
this purpose.  This tag is normally a pointer to the table of primitive
operations for the type (see ARM 3.9(3)).  The programmer can query and
use the tag, see ARM 3.9(18.1/2 and following), also indirectly through
streams, see ARM 13.13.2(31/2, 34/2).

Constants with other purposes can be declared outside the type
definition and thus shared among all objects of the declared type.

-- 
Ludovic Brenta.



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

* Re: Why constant components are not permitted ?
  2011-11-24 19:06 ` anon
@ 2011-11-24 19:49   ` David Sauvage
  2011-11-24 22:55     ` Georg Bauhaus
  2011-11-24 23:53     ` anon
  0 siblings, 2 replies; 27+ messages in thread
From: David Sauvage @ 2011-11-24 19:49 UTC (permalink / raw)


On 24 nov, 23:06, a...@att.net wrote:
>   type test is record
>                  a : integer := 16#FFFF# ;
>                  b : string ( 1 .. 4 ) := "this" ; -- is OK
>                  c : string := "is" ; -- error because no define boundary
>                end record ;
>
> Reason no one ask the ARG.

ok, but the following is also not permitted

type Object is record
   Tag : constant Positive := 42; -- GNAT compilation failed :
constant components are not permitted
end record;

There is something special about constant components, no related to
unconstrained types in record.


Cheers



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

* Re: Why constant components are not permitted ?
  2011-11-24 19:49   ` David Sauvage
@ 2011-11-24 22:55     ` Georg Bauhaus
  2011-11-24 23:53     ` anon
  1 sibling, 0 replies; 27+ messages in thread
From: Georg Bauhaus @ 2011-11-24 22:55 UTC (permalink / raw)


On 24.11.11 20:49, David Sauvage wrote:

> There is something special about constant components, no related to
> unconstrained types in record.

Just for the record, single array components are not constant,
either. These two may be solutions:

    type Object
       (Tag : Positive)
    is record
       ...;
    end record

    generic
       Tag : in Positive;
    package Object
     is
       ...;
    end Object;




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

* Re: Why constant components are not permitted ?
  2011-11-24 19:49   ` David Sauvage
  2011-11-24 22:55     ` Georg Bauhaus
@ 2011-11-24 23:53     ` anon
  1 sibling, 0 replies; 27+ messages in thread
From: anon @ 2011-11-24 23:53 UTC (permalink / raw)



Unlike C which allow constants ("#define") any where a programmer 
chooses. Ada is more structured. Following Ada RM "3.8 Record types" 
there are no constants allowed within a defintion of a record.


RM 3.8 Record Types

  2   record_type_definition ::= [[abstract] tagged] [limited] 
                                 record_definition

  3   record_definition ::=
          record
             component_list
          end record
        | null record

  4   component_list ::=
            component_item {component_item}
         | {component_item} variant_part
         |  null;

  5   component_item ::= component_declaration | representation_clause

  6   component_declaration ::=
         defining_identifier_list : component_definition 
                                       [:= default_expression];

from RM 3.6 Array Type ;

  7   component_definition ::= [aliased] subtype_indication


from RM 3.2.2 Subtype Declarations

  3   subtype_indication ::=  subtype_mark [constraint]

  4   subtype_mark ::= subtype_name


And even though it is not define in the RM BNF a:

      subtype_name ::= simple_name


So, Component_definition does not allow the keyword "constant". So it 
is illegal within the definition of a record.



In <9dac0cd0-2142-4e2c-b049-3e76a1dcea2c@w1g2000vba.googlegroups.com>, David Sauvage <david.sauvage@adalabs.com> writes:
>On 24 nov, 23:06, a...@att.net wrote:
>> =A0 type test is record
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0a : integer :=3D 16#FFFF# ;
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0b : string ( 1 .. 4 ) :=3D "this" ; --=
> is OK
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0c : string :=3D "is" ; -- error becaus=
>e no define boundary
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0end record ;
>>
>> Reason no one ask the ARG.
>
>ok, but the following is also not permitted
>
>type Object is record
>   Tag : constant Positive :=3D 42; -- GNAT compilation failed :
>constant components are not permitted
>end record;
>
>There is something special about constant components, no related to
>unconstrained types in record.
>
>
>Cheers




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

* Re: Why constant components are not permitted ?
  2011-11-24 18:14 Why constant components are not permitted ? David Sauvage
  2011-11-24 19:06 ` anon
  2011-11-24 19:46 ` Ludovic Brenta
@ 2011-11-25  6:56 ` Niklas Holsti
  2011-11-25  9:53   ` Yannick Duchêne (Hibou57)
  2011-11-25  7:03 ` AdaMagica
  2011-11-25  9:12 ` Dmitry A. Kazakov
  4 siblings, 1 reply; 27+ messages in thread
From: Niklas Holsti @ 2011-11-25  6:56 UTC (permalink / raw)


On 11-11-24 19:14 , David Sauvage wrote:
> Hello,
>
> given the following :
>
> type Object is record
>     Tag : constant String := "key"; -- GNAT compilation failed :
> constant components are not permitted
> end record;
>
> In my case I find it useful to declare my Object Tag component as a
> constant, but it is not permitted, I would be interested to learn
> why ?

It seems no earlier responder has understood why you want to have a 
constant component, nor do I, so I'll ask directly: Why do you think a 
constant component would be  useful?

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



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

* Re: Why constant components are not permitted ?
  2011-11-24 18:14 Why constant components are not permitted ? David Sauvage
                   ` (2 preceding siblings ...)
  2011-11-25  6:56 ` Niklas Holsti
@ 2011-11-25  7:03 ` AdaMagica
  2011-11-25  9:12 ` Dmitry A. Kazakov
  4 siblings, 0 replies; 27+ messages in thread
From: AdaMagica @ 2011-11-25  7:03 UTC (permalink / raw)


Just make the component a discriminant, then it's constant (as Georg
has proposed).

type Rec (Discriminant: Some_Discrete_Type) is record
  ...  -- variable components
end record;



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

* Re: Why constant components are not permitted ?
  2011-11-24 19:46 ` Ludovic Brenta
@ 2011-11-25  9:10   ` Dmitry A. Kazakov
  2011-11-25 10:23     ` Ludovic Brenta
  0 siblings, 1 reply; 27+ messages in thread
From: Dmitry A. Kazakov @ 2011-11-25  9:10 UTC (permalink / raw)


On Thu, 24 Nov 2011 20:46:13 +0100, Ludovic Brenta wrote:

> Constants with other purposes can be declared outside the type
> definition and thus shared among all objects of the declared type.

The constant value may depend on the enclosing object, undesired or
impossible to re-evaluate each time when needed.

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



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

* Re: Why constant components are not permitted ?
  2011-11-24 18:14 Why constant components are not permitted ? David Sauvage
                   ` (3 preceding siblings ...)
  2011-11-25  7:03 ` AdaMagica
@ 2011-11-25  9:12 ` Dmitry A. Kazakov
  2011-11-25  9:57   ` Yannick Duchêne (Hibou57)
  4 siblings, 1 reply; 27+ messages in thread
From: Dmitry A. Kazakov @ 2011-11-25  9:12 UTC (permalink / raw)


On Thu, 24 Nov 2011 10:14:34 -0800 (PST), David Sauvage wrote:

> In my case I find it useful to declare my Object Tag component as a
> constant, but it is not permitted, I would be interested to learn
> why ?

Language irregularity. There are other things which work for components
differently, e.g. no anonymous arrays.

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



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

* Re: Why constant components are not permitted ?
  2011-11-25  6:56 ` Niklas Holsti
@ 2011-11-25  9:53   ` Yannick Duchêne (Hibou57)
  2011-11-25 10:06     ` AdaMagica
  0 siblings, 1 reply; 27+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2011-11-25  9:53 UTC (permalink / raw)


Le Fri, 25 Nov 2011 07:56:03 +0100, Niklas Holsti  
<niklas.holsti@tidorum.invalid> a écrit:

> On 11-11-24 19:14 , David Sauvage wrote:
>> Hello,
>>
>> given the following :
>>
>> type Object is record
>>     Tag : constant String := "key"; -- GNAT compilation failed :
>> constant components are not permitted
>> end record;
>>
>> In my case I find it useful to declare my Object Tag component as a
>> constant, but it is not permitted, I would be interested to learn
>> why ?
>
> It seems no earlier responder has understood why you want to have a  
> constant component, nor do I, so I'll ask directly: Why do you think a  
> constant component would be  useful?
The same question for me. A record, is not an interface, that's an  
implementation solution. One want to use a record to store some aggregated  
stuff, thus its name, “record”.

If a type T is implemented as a record, the interface should not expose  
the record (as with any implementation details, except if part of an  
expected specification). If T is to be associated with a constant, the  
constant could be implemented as a function getting a T as parameter, and  
returning that constant.

Unless the OP was to reinvent a wheel already provided by Ada, which is  
record discriminant. If that constant is a kind of characterization of  
instances of T, then a discriminant is the obvious choice.

-- 
“Syntactic sugar causes cancer of the semi-colons.” [1]
“Structured Programming supports the law of the excluded muddle.” [1]
[1]: [Epigrams on Programming — Alan J. — P. Yale University]



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

* Re: Why constant components are not permitted ?
  2011-11-25  9:12 ` Dmitry A. Kazakov
@ 2011-11-25  9:57   ` Yannick Duchêne (Hibou57)
  2011-11-25 10:22     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 27+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2011-11-25  9:57 UTC (permalink / raw)


Le Fri, 25 Nov 2011 10:12:54 +0100, Dmitry A. Kazakov  
<mailbox@dmitry-kazakov.de> a écrit:

> On Thu, 24 Nov 2011 10:14:34 -0800 (PST), David Sauvage wrote:
>
>> In my case I find it useful to declare my Object Tag component as a
>> constant, but it is not permitted, I would be interested to learn
>> why ?
>
> Language irregularity. There are other things which work for components
> differently, e.g. no anonymous arrays.
You could argue it's an irregularity, if this was useful to anything.  
Another one could argue Ada did not blindly apply any schema to anything.  
A record is not a general purpose declaration scope; there are mainly  
packages and generic packages for that purpose.


-- 
“Syntactic sugar causes cancer of the semi-colons.” [1]
“Structured Programming supports the law of the excluded muddle.” [1]
[1]: [Epigrams on Programming — Alan J. — P. Yale University]



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

* Re: Why constant components are not permitted ?
  2011-11-25  9:53   ` Yannick Duchêne (Hibou57)
@ 2011-11-25 10:06     ` AdaMagica
  2011-11-25 10:16       ` AdaMagica
                         ` (3 more replies)
  0 siblings, 4 replies; 27+ messages in thread
From: AdaMagica @ 2011-11-25 10:06 UTC (permalink / raw)


Useful applications for constant components could be (this is what you
can have in C++):

record  -- *not* Ada
  Name: constant String   := "James Bond";
  Id  : constant Positive := 007;  -- e.g employee number of a company
  other variable components
end record;

In Ada, you hide the components (private record), define a constructor
function providing the values for the constant items and provide only
read operations for them.



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

* Re: Why constant components are not permitted ?
  2011-11-25 10:06     ` AdaMagica
@ 2011-11-25 10:16       ` AdaMagica
  2011-11-25 10:56       ` Yannick Duchêne (Hibou57)
                         ` (2 subsequent siblings)
  3 siblings, 0 replies; 27+ messages in thread
From: AdaMagica @ 2011-11-25 10:16 UTC (permalink / raw)


> In Ada, you hide the components (private record), define a constructor
> function providing the values for the constant items and provide only
> read operations for them.

Then you are safe against inadvertant component changes by the client,
but inside, where you have full visibility, you can inadvertantly
change components which should be constants.



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

* Re: Why constant components are not permitted ?
  2011-11-25  9:57   ` Yannick Duchêne (Hibou57)
@ 2011-11-25 10:22     ` Dmitry A. Kazakov
  2011-11-25 11:00       ` Yannick Duchêne (Hibou57)
  0 siblings, 1 reply; 27+ messages in thread
From: Dmitry A. Kazakov @ 2011-11-25 10:22 UTC (permalink / raw)


On Fri, 25 Nov 2011 10:57:25 +0100, Yannick Duch�ne (Hibou57) wrote:

> Le Fri, 25 Nov 2011 10:12:54 +0100, Dmitry A. Kazakov  
> <mailbox@dmitry-kazakov.de> a �crit:
> 
>> On Thu, 24 Nov 2011 10:14:34 -0800 (PST), David Sauvage wrote:
>>
>>> In my case I find it useful to declare my Object Tag component as a
>>> constant, but it is not permitted, I would be interested to learn
>>> why ?
>>
>> Language irregularity. There are other things which work for components
>> differently, e.g. no anonymous arrays.
> You could argue it's an irregularity, if this was useful to anything.

No, irregularity is independent on usability. abs might look totally
useless for modular types, yet its presence makes the language more
regular. [abs is not useless in the context generic programming is
considered]
  
> Another one could argue Ada did not blindly apply any schema to anything.

The road to hell is paved with good intentions...

The rule of good language design is: do not introduce irregularities and
constraints unless absolutely necessary.

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



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

* Re: Why constant components are not permitted ?
  2011-11-25  9:10   ` Dmitry A. Kazakov
@ 2011-11-25 10:23     ` Ludovic Brenta
  2011-11-25 10:45       ` Dmitry A. Kazakov
  0 siblings, 1 reply; 27+ messages in thread
From: Ludovic Brenta @ 2011-11-25 10:23 UTC (permalink / raw)


Dmitry A. Kazakov wrote on comp.lang.ada:
> On Thu, 24 Nov 2011 20:46:13 +0100, Ludovic Brenta wrote:
> > Constants with other purposes can be declared outside the type
> > definition and thus shared among all objects of the declared type.
>
> The constant value may depend on the enclosing object, undesired or
> impossible to re-evaluate each time when needed.

IOW, the constant is the cached result of a function taking the rest,
or part thereof, of the object as its sole parameter.  If this result
is a constant, this can only mean that the rest of the object is
constant too, right? In this case one can say:

type T is record
   Hash_Value : Natural;
   Component_1 : Foo;
   Component_2 : Bar;
end record;

Object : constant T := (Hash_Value => Hash (Component_1, Component_2),
                        Component_1 => Component_1,
                        Component_2 => Component_2);

If not all other components are inputs into the function returning
the constant result, then it is probably desirable to split the object
into two types, such that all the "constant" components are in a type
of their own:

type Immutable_Part is record
   Hash_Value : Natural;
   Component_1 : Foo;
end record;

type T (Immutable : not null access constant Immutable_Part) is record
   Component_2 : Bar;
end record;

--
Ludovic Brenta.



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

* Re: Why constant components are not permitted ?
  2011-11-25 10:23     ` Ludovic Brenta
@ 2011-11-25 10:45       ` Dmitry A. Kazakov
  0 siblings, 0 replies; 27+ messages in thread
From: Dmitry A. Kazakov @ 2011-11-25 10:45 UTC (permalink / raw)


On Fri, 25 Nov 2011 02:23:22 -0800 (PST), Ludovic Brenta wrote:

> Dmitry A. Kazakov wrote on comp.lang.ada:
>> On Thu, 24 Nov 2011 20:46:13 +0100, Ludovic Brenta wrote:
>>> Constants with other purposes can be declared outside the type
>>> definition and thus shared among all objects of the declared type.
>>
>> The constant value may depend on the enclosing object, undesired or
>> impossible to re-evaluate each time when needed.
> 
> IOW, the constant is the cached result of a function taking the rest,
> or part thereof, of the object as its sole parameter.

and possibly the context where the object was created.

> If this result
> is a constant, this can only mean that the rest of the object is
> constant too, right?

No. The object itself can be mutable. Otherwise we would need no
discriminants, which among other play the role of constant components.

> If not all other components are inputs into the function returning
> the constant result, then it is probably desirable to split the object
> into two types, such that all the "constant" components are in a type
> of their own:
> 
> type Immutable_Part is record
>    Hash_Value : Natural;
>    Component_1 : Foo;
> end record;
> 
> type T (Immutable : not null access constant Immutable_Part) is record
>    Component_2 : Bar;
> end record;

That would complicate design. If discriminants must keep on playing the
role of constant components, then Ada should allow any objects as
discriminants:

   type T (Immutable : constant Immutable_Part) is record
      Component_2 : Bar;
   end record;

However I would prefer discriminants used as constraints and only
constraints in order to produce constrained subtypes.

Furthermore, since Ada does not have abstract record interface (getter and
setter), you might also with to have this:

   type T is private with record
      Field_1 : constant Integer;  -- You cannot change this
      Field_2 : constant Integer;  -- You cannot change this either
   end record;
private
   type T is record
      Field_1 : constant Integer := 3; -- Nobody can change this field
      Field_2 : Integer; -- I can change this, you cannot
      Field_3 : Integer; -- You cannot see this one
   end record;

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



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

* Re: Why constant components are not permitted ?
  2011-11-25 10:06     ` AdaMagica
  2011-11-25 10:16       ` AdaMagica
@ 2011-11-25 10:56       ` Yannick Duchêne (Hibou57)
  2011-11-25 17:41       ` Niklas Holsti
  2011-11-26 10:22       ` Pascal Obry
  3 siblings, 0 replies; 27+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2011-11-25 10:56 UTC (permalink / raw)


Le Fri, 25 Nov 2011 11:06:43 +0100, AdaMagica  
<christ-usch.grein@t-online.de> a écrit:

> Useful applications for constant components could be (this is what you
> can have in C++):
>
> record  -- *not* Ada
>   Name: constant String   := "James Bond";
>   Id  : constant Positive := 007;  -- e.g employee number of a company
>   other variable components
> end record;
And your record type is supposed to have a single instance ? Or else, if  
it is expected to have multiple instances, I feel to see a trouble.

With apologizes if on the other hand, I missed what you meant.

-- 
“Syntactic sugar causes cancer of the semi-colons.” [1]
“Structured Programming supports the law of the excluded muddle.” [1]
[1]: [Epigrams on Programming — Alan J. — P. Yale University]



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

* Re: Why constant components are not permitted ?
  2011-11-25 10:22     ` Dmitry A. Kazakov
@ 2011-11-25 11:00       ` Yannick Duchêne (Hibou57)
  0 siblings, 0 replies; 27+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2011-11-25 11:00 UTC (permalink / raw)


Le Fri, 25 Nov 2011 11:22:44 +0100, Dmitry A. Kazakov  
<mailbox@dmitry-kazakov.de> a écrit:
>> Another one could argue Ada did not blindly apply any schema to  
>> anything.
>
> The road to hell is paved with good intentions...
>
> The rule of good language design is: do not introduce irregularities and
> constraints unless absolutely necessary.
I can suggest you another good intention “to hell”: this kind of  
constraint may help to point design or specification troubles.


-- 
“Syntactic sugar causes cancer of the semi-colons.” [1]
“Structured Programming supports the law of the excluded muddle.” [1]
[1]: [Epigrams on Programming — Alan J. — P. Yale University]



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

* Re: Why constant components are not permitted ?
  2011-11-25 10:06     ` AdaMagica
  2011-11-25 10:16       ` AdaMagica
  2011-11-25 10:56       ` Yannick Duchêne (Hibou57)
@ 2011-11-25 17:41       ` Niklas Holsti
  2011-11-26  0:23         ` anon
  2011-11-29  4:10         ` Randy Brukardt
  2011-11-26 10:22       ` Pascal Obry
  3 siblings, 2 replies; 27+ messages in thread
From: Niklas Holsti @ 2011-11-25 17:41 UTC (permalink / raw)


On 11-11-25 11:06 , AdaMagica wrote:
> Useful applications for constant components could be (this is what you
> can have in C++):
>
> record  -- *not* Ada
>    Name: constant String   := "James Bond";
>    Id  : constant Positive := 007;  -- e.g employee number of a company
>    other variable components
> end record;

The original poster specified the constant value in the declaration of 
the record *type*, not separately for each record *object*. This is what 
I do not understand: what is the use of a value that is the same for all 
objects in which it can appear?

The "type constants" that Ada now provides are the attributes of a type 
(tag, size, and so on), as Ludovic pointed out.

I understand that some would like to have a "write once" kind of record 
component that can be given a value once, when a record object is 
created, and cannot be changed afterwards. Discriminants are like that 
in Ada, but cannot be of arbitrary type.

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



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

* Re: Why constant components are not permitted ?
  2011-11-25 17:41       ` Niklas Holsti
@ 2011-11-26  0:23         ` anon
  2011-11-29  4:10         ` Randy Brukardt
  1 sibling, 0 replies; 27+ messages in thread
From: anon @ 2011-11-26  0:23 UTC (permalink / raw)


--
--  To use constant with "record" define record then create an object 
--  with preset value that contains the key word "constant"
--

procedure a is

  type test_type is record
                 a : integer ;
                 b : string ( 1 .. 4 ) := "this" ; -- default
                 c : string ( 1 .. 10 ) ;
               end record ;


  -- tst is a constant record with all values are unchangable
  
  tst : constant test_type := test_type ' ( 16#FFFF# , 
                                            "that", 
                                            "0123456789" ) ; 

begin
  null ;
end ;



In <9ja2aiFla6U1@mid.individual.net>, Niklas Holsti <niklas.holsti@tidorum.invalid> writes:
>On 11-11-25 11:06 , AdaMagica wrote:
>> Useful applications for constant components could be (this is what you
>> can have in C++):
>>
>> record  -- *not* Ada
>>    Name: constant String   := "James Bond";
>>    Id  : constant Positive := 007;  -- e.g employee number of a company
>>    other variable components
>> end record;
>
>The original poster specified the constant value in the declaration of 
>the record *type*, not separately for each record *object*. This is what 
>I do not understand: what is the use of a value that is the same for all 
>objects in which it can appear?
>
>The "type constants" that Ada now provides are the attributes of a type 
>(tag, size, and so on), as Ludovic pointed out.
>
>I understand that some would like to have a "write once" kind of record 
>component that can be given a value once, when a record object is 
>created, and cannot be changed afterwards. Discriminants are like that 
>in Ada, but cannot be of arbitrary type.
>
>-- 
>Niklas Holsti
>Tidorum Ltd
>niklas holsti tidorum fi
>       .      @       .




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

* Re: Why constant components are not permitted ?
  2011-11-25 10:06     ` AdaMagica
                         ` (2 preceding siblings ...)
  2011-11-25 17:41       ` Niklas Holsti
@ 2011-11-26 10:22       ` Pascal Obry
  2011-11-26 10:59         ` Dmitry A. Kazakov
  3 siblings, 1 reply; 27+ messages in thread
From: Pascal Obry @ 2011-11-26 10:22 UTC (permalink / raw)
  To: AdaMagica

Le 25/11/2011 11:06, AdaMagica a �crit :
> Useful applications for constant components could be (this is what you
> can have in C++):
> 
> record  -- *not* Ada
>   Name: constant String   := "James Bond";
>   Id  : constant Positive := 007;  -- e.g employee number of a company
>   other variable components
> end record;
> 
> In Ada, you hide the components (private record), define a constructor
> function providing the values for the constant items and provide only
> read operations for them.

But why on earth do you want a field in a record with the same value for
all objects of this type?

This looks like an application wide constant, so just declare the
constant value outside of the record.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|    http://www.obry.net  -  http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B




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

* Re: Why constant components are not permitted ?
  2011-11-26 10:22       ` Pascal Obry
@ 2011-11-26 10:59         ` Dmitry A. Kazakov
  0 siblings, 0 replies; 27+ messages in thread
From: Dmitry A. Kazakov @ 2011-11-26 10:59 UTC (permalink / raw)


On Sat, 26 Nov 2011 11:22:35 +0100, Pascal Obry wrote:

> Le 25/11/2011 11:06, AdaMagica a �crit :
>> Useful applications for constant components could be (this is what you
>> can have in C++):
>> 
>> record  -- *not* Ada
>>   Name: constant String   := "James Bond";
>>   Id  : constant Positive := 007;  -- e.g employee number of a company
>>   other variable components
>> end record;
>> 
>> In Ada, you hide the components (private record), define a constructor
>> function providing the values for the constant items and provide only
>> read operations for them.
> 
> But why on earth do you want a field in a record with the same value for
> all objects of this type?

   record -- This is not Ada!
      Name : constant Unbounded_String := Read (File);

Of course it should be possible to initialize constant fields upon object
construction:

   record -- This is not Ada!
      Name : constant Unbounded_String;  -- Deferred until construction

Also don't forget constant views of fields which are privately
non-constant.

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



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

* Re: Why constant components are not permitted ?
  2011-11-25 17:41       ` Niklas Holsti
  2011-11-26  0:23         ` anon
@ 2011-11-29  4:10         ` Randy Brukardt
  2011-11-29  7:55           ` David Sauvage
  1 sibling, 1 reply; 27+ messages in thread
From: Randy Brukardt @ 2011-11-29  4:10 UTC (permalink / raw)


"Niklas Holsti" <niklas.holsti@tidorum.invalid> wrote in message 
news:9ja2aiFla6U1@mid.individual.net...
...
> The original poster specified the constant value in the declaration of the 
> record *type*, not separately for each record *object*. This is what I do 
> not understand: what is the use of a value that is the same for all 
> objects in which it can appear?

One such use is in interfacing when some components always have fixed 
values. For instance, most record types in Win32 have a Size component that 
is always the same for a particular type declaration. It would be nice to 
declare that as a constant (since it cannot change).

But this need is so rare and unusual it hardly makes sense to add language 
support for it. The story would be different for a new from-scratch language 
design; the omission looks like a mistake in Ada and it surely would be more 
consistent to have it even if it is rarely used. But redoing the Standard 
and existing implementations needs a better reason than "more consistent but 
rarely used".

                                 Randy. 





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

* Re: Why constant components are not permitted ?
  2011-11-29  4:10         ` Randy Brukardt
@ 2011-11-29  7:55           ` David Sauvage
  2011-11-29 10:55             ` Yannick Duchêne (Hibou57)
  2011-11-29 11:17             ` Mark Lorenzen
  0 siblings, 2 replies; 27+ messages in thread
From: David Sauvage @ 2011-11-29  7:55 UTC (permalink / raw)


On 29 nov, 08:10, "Randy Brukardt" <ra...@rrsoftware.com> wrote:
> "Niklas Holsti" <niklas.hol...@tidorum.invalid> wrote in message
>
> news:9ja2aiFla6U1@mid.individual.net...
> ...
>
> > The original poster specified the constant value in the declaration of the
> > record *type*, not separately for each record *object*. This is what I do
> > not understand: what is the use of a value that is the same for all
> > objects in which it can appear?
>
> One such use is in interfacing when some components always have fixed
> values. For instance, most record types in Win32 have a Size component that
> is always the same for a particular type declaration. It would be nice to
> declare that as a constant (since it cannot change).
>
> But this need is so rare and unusual it hardly makes sense to add language
> support for it. The story would be different for a new from-scratch language
> design; the omission looks like a mistake in Ada and it surely would be more
> consistent to have it even if it is rarely used. But redoing the Standard
> and existing implementations needs a better reason than "more consistent but
> rarely used".
>
>                                  Randy.


Why use a constant attribute in a record ?

To expose my design intention in the code.

I will have a big tagged types hierarchy, and wanted to declare a read
only component in each root tagged types, so that when users will use
the framework and add child types, they will not been able to modify
this attribute.
I would not been able to do this using a primitive method as they
would be able to override it (think of the final keywork in Java to
prevent a method from being overridden).

Anyway, my question concern the rational of why this is not possible
in Ada, and I got some answers.

Thanks to all for your inputs.

Cheers,




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

* Re: Why constant components are not permitted ?
  2011-11-29  7:55           ` David Sauvage
@ 2011-11-29 10:55             ` Yannick Duchêne (Hibou57)
  2011-11-29 11:17             ` Mark Lorenzen
  1 sibling, 0 replies; 27+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2011-11-29 10:55 UTC (permalink / raw)


Le Tue, 29 Nov 2011 08:55:49 +0100, David Sauvage  
<david.sauvage@adalabs.com> a écrit:
> I will have a big tagged types hierarchy, and wanted to declare a read
> only component in each root tagged types, so that when users will use
> the framework and add child types, they will not been able to modify
> this attribute.
> I would not been able to do this using a primitive method as they
> would be able to override it (think of the final keywork in Java to
> prevent a method from being overridden).
What about this:

     function Constant_Property (Instance : Instance_Type'Class) return  
Property_Type;

Is that a relevant option ?

-- 
“Syntactic sugar causes cancer of the semi-colons.” [1]
“Structured Programming supports the law of the excluded muddle.” [1]
[1]: [Epigrams on Programming — Alan J. — P. Yale University]



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

* Re: Why constant components are not permitted ?
  2011-11-29  7:55           ` David Sauvage
  2011-11-29 10:55             ` Yannick Duchêne (Hibou57)
@ 2011-11-29 11:17             ` Mark Lorenzen
  1 sibling, 0 replies; 27+ messages in thread
From: Mark Lorenzen @ 2011-11-29 11:17 UTC (permalink / raw)


On 29 Nov., 08:55, David Sauvage <david.sauv...@adalabs.com> wrote:

> I will have a big tagged types hierarchy, and wanted to declare a read
> only component in each root tagged types, so that when users will use
> the framework and add child types, they will not been able to modify
> this attribute.

Can't you use the Tag attribute for this?

Regards,

Mark L



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

end of thread, other threads:[~2011-11-29 11:20 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-24 18:14 Why constant components are not permitted ? David Sauvage
2011-11-24 19:06 ` anon
2011-11-24 19:49   ` David Sauvage
2011-11-24 22:55     ` Georg Bauhaus
2011-11-24 23:53     ` anon
2011-11-24 19:46 ` Ludovic Brenta
2011-11-25  9:10   ` Dmitry A. Kazakov
2011-11-25 10:23     ` Ludovic Brenta
2011-11-25 10:45       ` Dmitry A. Kazakov
2011-11-25  6:56 ` Niklas Holsti
2011-11-25  9:53   ` Yannick Duchêne (Hibou57)
2011-11-25 10:06     ` AdaMagica
2011-11-25 10:16       ` AdaMagica
2011-11-25 10:56       ` Yannick Duchêne (Hibou57)
2011-11-25 17:41       ` Niklas Holsti
2011-11-26  0:23         ` anon
2011-11-29  4:10         ` Randy Brukardt
2011-11-29  7:55           ` David Sauvage
2011-11-29 10:55             ` Yannick Duchêne (Hibou57)
2011-11-29 11:17             ` Mark Lorenzen
2011-11-26 10:22       ` Pascal Obry
2011-11-26 10:59         ` Dmitry A. Kazakov
2011-11-25  7:03 ` AdaMagica
2011-11-25  9:12 ` Dmitry A. Kazakov
2011-11-25  9:57   ` Yannick Duchêne (Hibou57)
2011-11-25 10:22     ` Dmitry A. Kazakov
2011-11-25 11:00       ` Yannick Duchêne (Hibou57)

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