comp.lang.ada
 help / color / mirror / Atom feed
* Question on variant/discriminated records
@ 2020-12-31 10:38 reinert
  2020-12-31 10:52 ` J-P. Rosen
  2020-12-31 15:03 ` Dmitry A. Kazakov
  0 siblings, 2 replies; 9+ messages in thread
From: reinert @ 2020-12-31 10:38 UTC (permalink / raw)


Hello,

Assume the following program and change "y1" to "y". 

procedure test3 is
 type options1_Type is (a,b,c);
 type rec1_Type(option1 : options1_Type) is
   record
      x : Float;
      case option1 is
        when a => null;
        when b => y : float;
        when c => y1,z : float;
      end case;
   end record;
begin
   null;
end test3;

My compiler does not like this change.  I find it not natural to invent
a new name "y1" instead of "y". What should I do?

reinert

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

* Re: Question on variant/discriminated records
  2020-12-31 10:38 Question on variant/discriminated records reinert
@ 2020-12-31 10:52 ` J-P. Rosen
  2020-12-31 12:24   ` reinert
  2020-12-31 15:03 ` Dmitry A. Kazakov
  1 sibling, 1 reply; 9+ messages in thread
From: J-P. Rosen @ 2020-12-31 10:52 UTC (permalink / raw)


Le 31/12/2020 à 11:38, reinert a écrit :
> Assume the following program and change "y1" to "y". 
> 
> procedure test3 is
>  type options1_Type is (a,b,c);
>  type rec1_Type(option1 : options1_Type) is
>    record
>       x : Float;
>       case option1 is
>         when a => null;
>         when b => y : float;
>         when c => y1,z : float;
>       end case;
>    end record;
> begin
>    null;
> end test3;
> 
> My compiler does not like this change.  I find it not natural to invent
> a new name "y1" instead of "y". What should I do?
> 
Yes, it is a rule that identifiers in parallel branches cannot be
identical. And there is a good reason for this: in Ada, when you access
a component, the compiler checks that the component exists, according to
the value of the discriminant. Assume that you write (if V is of type
rec1_type):
   V.Y := 1.0;

If both components are named Y, should the discriminant be B or C?
-- 
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] 9+ messages in thread

* Re: Question on variant/discriminated records
  2020-12-31 10:52 ` J-P. Rosen
@ 2020-12-31 12:24   ` reinert
  2020-12-31 12:46     ` Gautier write-only address
  0 siblings, 1 reply; 9+ messages in thread
From: reinert @ 2020-12-31 12:24 UTC (permalink / raw)


Here must be a hole in my understanding. I thought that the discriminant must be known
before V.Y := 1.0; and hence there should be no confusion?

reinert


torsdag 31. desember 2020 kl. 11:52:26 UTC+1 skrev J-P. Rosen:

> Yes, it is a rule that identifiers in parallel branches cannot be 
> identical. And there is a good reason for this: in Ada, when you access 
> a component, the compiler checks that the component exists, according to 
> the value of the discriminant. Assume that you write (if V is of type 
> rec1_type): 
> V.Y := 1.0; 
> 
> If both components are named Y, should the discriminant be B or C? 
> -- 
> 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] 9+ messages in thread

* Re: Question on variant/discriminated records
  2020-12-31 12:24   ` reinert
@ 2020-12-31 12:46     ` Gautier write-only address
  2020-12-31 12:57       ` reinert
  2020-12-31 13:29       ` DrPi
  0 siblings, 2 replies; 9+ messages in thread
From: Gautier write-only address @ 2020-12-31 12:46 UTC (permalink / raw)


On Thursday, December 31, 2020 at 1:24:25 PM UTC+1, reinert wrote:
> Here must be a hole in my understanding. I thought that the discriminant must be known 
> before V.Y := 1.0; and hence there should be no confusion? 

Imagine you have a function Evaluate (r : rec1_Type) return Float.
Except in trivial cases like
  if r.option1 = b then result := result + r.y; end if;
the compiler cannot guess the value of the discriminant.

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

* Re: Question on variant/discriminated records
  2020-12-31 12:46     ` Gautier write-only address
@ 2020-12-31 12:57       ` reinert
  2020-12-31 13:29       ` DrPi
  1 sibling, 0 replies; 9+ messages in thread
From: reinert @ 2020-12-31 12:57 UTC (permalink / raw)


Approaching :-) But the interpretation of (the meaning of) r.y could in principle be 
calculated on the fly (when called) based on the received discriminant -
but arguments related to  security/reliability  are against?

Maybe nice to find errors at compile time as compared to experiencing costly runtime errors :-)

reinert

torsdag 31. desember 2020 kl. 13:46:47 UTC+1 skrev gautier:
> On Thursday, December 31, 2020 at 1:24:25 PM UTC+1, reinert wrote: 
> > Here must be a hole in my understanding. I thought that the discriminant must be known 
> > before V.Y := 1.0; and hence there should be no confusion?
> Imagine you have a function Evaluate (r : rec1_Type) return Float. 
> Except in trivial cases like 
> if r.option1 = b then result := result + r.y; end if; 
> the compiler cannot guess the value of the discriminant.

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

* Re: Question on variant/discriminated records
  2020-12-31 12:46     ` Gautier write-only address
  2020-12-31 12:57       ` reinert
@ 2020-12-31 13:29       ` DrPi
  2020-12-31 16:06         ` Gautier write-only address
  1 sibling, 1 reply; 9+ messages in thread
From: DrPi @ 2020-12-31 13:29 UTC (permalink / raw)


Le 31/12/2020 à 13:46, Gautier write-only address a écrit :
> On Thursday, December 31, 2020 at 1:24:25 PM UTC+1, reinert wrote:
>> Here must be a hole in my understanding. I thought that the discriminant must be known
>> before V.Y := 1.0; and hence there should be no confusion?
> 
> Imagine you have a function Evaluate (r : rec1_Type) return Float.
> Except in trivial cases like
>    if r.option1 = b then result := result + r.y; end if;
> the compiler cannot guess the value of the discriminant.
> 
Isn't the discriminant stored in the record ?

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

* Re: Question on variant/discriminated records
  2020-12-31 10:38 Question on variant/discriminated records reinert
  2020-12-31 10:52 ` J-P. Rosen
@ 2020-12-31 15:03 ` Dmitry A. Kazakov
  2021-01-01  9:19   ` reinert
  1 sibling, 1 reply; 9+ messages in thread
From: Dmitry A. Kazakov @ 2020-12-31 15:03 UTC (permalink / raw)


On 2020-12-31 11:38, reinert wrote:

> Assume the following program and change "y1" to "y".
> 
> procedure test3 is
>   type options1_Type is (a,b,c);
>   type rec1_Type(option1 : options1_Type) is
>     record
>        x : Float;
>        case option1 is
>          when a => null;
>          when b => y : float;
>          when c => y1,z : float;
>        end case;
>     end record;
> begin
>     null;
> end test3;
> 
> My compiler does not like this change.  I find it not natural to invent
> a new name "y1" instead of "y". What should I do?

Use nested choice:

    type rec1_Type (option1 : options1_Type) is record
       x : Float;
       case option1 is
          when a =>
             null;
          when b | c =>
             y : float;
             case option1 is
                when c =>
                   z : float;
                when others =>
                   null;
             end case;
       end case;
    end record;

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

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

* Re: Question on variant/discriminated records
  2020-12-31 13:29       ` DrPi
@ 2020-12-31 16:06         ` Gautier write-only address
  0 siblings, 0 replies; 9+ messages in thread
From: Gautier write-only address @ 2020-12-31 16:06 UTC (permalink / raw)


> > Imagine you have a function Evaluate (r : rec1_Type) return Float. 
> > Except in trivial cases like 
> > if r.option1 = b then result := result + r.y; end if; 
> > the compiler cannot guess the value of the discriminant. 
> >
> Isn't the discriminant stored in the record ?

Yes. But if both "y"s were allowed, the compiler should for the non-trivial cases, generate a hidden "case" instruction to select the right "y" depending on the value of option1 (both "y"s are not necessarily at the same position in the record). Certainly the language designers have considered the additional complexity and performance cost of that point. Fortunately the nested choice as suggested by Dmitry solves the problem.

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

* Re: Question on variant/discriminated records
  2020-12-31 15:03 ` Dmitry A. Kazakov
@ 2021-01-01  9:19   ` reinert
  0 siblings, 0 replies; 9+ messages in thread
From: reinert @ 2021-01-01  9:19 UTC (permalink / raw)


Thanks, problem clarified and solved :-)

reinert

torsdag 31. desember 2020 kl. 16:03:57 UTC+1 skrev Dmitry A. Kazakov:

> Use nested choice: 
> 
....

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

end of thread, other threads:[~2021-01-01  9:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-31 10:38 Question on variant/discriminated records reinert
2020-12-31 10:52 ` J-P. Rosen
2020-12-31 12:24   ` reinert
2020-12-31 12:46     ` Gautier write-only address
2020-12-31 12:57       ` reinert
2020-12-31 13:29       ` DrPi
2020-12-31 16:06         ` Gautier write-only address
2020-12-31 15:03 ` Dmitry A. Kazakov
2021-01-01  9:19   ` reinert

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