comp.lang.ada
 help / color / mirror / Atom feed
* Basic issue with dotted notation and tagged types
@ 2012-10-21 22:55 Yannick Duchêne (Hibou57)
  2012-10-22  7:46 ` J-P. Rosen
  2012-10-22 10:41 ` AdaMagica
  0 siblings, 2 replies; 12+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2012-10-21 22:55 UTC (permalink / raw)


I can't remember if this was already discussed here, somewhere in a  
rationale or an RM comment, here is something I really don't like, with a  
field (or member) of a tagged type and a function, both of the same name;  
a very basic case:


     with Ada.Text_IO;

     procedure Test2 is

        package IO renames Ada.Text_IO;

        -- type T is tagged;
        -- function F (E : T'Class) return Character;

        type T
           is tagged record
              F : Character := 'A';
           end record;

        function F
          (E : T'Class)
           return Character
           is ('B');

        E : T;
        C : Character := E.F; -- The function or the field?
     begin
        IO.Put (C);
     end;


Will this display `A` or `B`? Actually, the program compiled with GNAT  
4.6, displays `A`. It displays the same even when the two commented lines  
are commented‑out and thus the function `F` is declared before the field  
`F`. I can't remember if the RM have special wordings about it, but I feel  
this does not honour the principle of least surprise. May be this should  
be disallowed, to access any one of the two, when both are together  
visible from a scope?


-- 
“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] 12+ messages in thread

* Re: Basic issue with dotted notation and tagged types
  2012-10-21 22:55 Basic issue with dotted notation and tagged types Yannick Duchêne (Hibou57)
@ 2012-10-22  7:46 ` J-P. Rosen
  2012-10-22  8:02   ` Dmitry A. Kazakov
  2012-10-22 10:52   ` Marius Amado-Alves
  2012-10-22 10:41 ` AdaMagica
  1 sibling, 2 replies; 12+ messages in thread
From: J-P. Rosen @ 2012-10-22  7:46 UTC (permalink / raw)


Le 22/10/2012 00:55, Yannick Duchêne (Hibou57) a écrit :
> [Snip]
> Will this display `A` or `B`? Actually, the program compiled with GNAT
> 4.6, displays `A`. It displays the same even when the two commented
> lines are commented‑out and thus the function `F` is declared before the
> field `F`. I can't remember if the RM have special wordings about it,
> but I feel this does not honour the principle of least surprise. May be
> this should be disallowed, to access any one of the two, when both are
> together visible from a scope?
> 
> 
RM2005 4.1.3 (9.2/2): "The designator of the subprogram shall not be the
same as that of a component of the tagged type visible at the point of
the selected_component."

BTW, this corresponds to /my/ view of least surprise: a prefixed view is
merely a writing simplification, and you are always free not to use it
without losing any functionality. OTOH, you would have no other way to
access a subcomponent. Therefore, it makes sense that the subcomponent
is "stronger".
-- 
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] 12+ messages in thread

* Re: Basic issue with dotted notation and tagged types
  2012-10-22  7:46 ` J-P. Rosen
@ 2012-10-22  8:02   ` Dmitry A. Kazakov
  2012-10-22 13:46     ` Yannick Duchêne (Hibou57)
  2012-10-22 10:52   ` Marius Amado-Alves
  1 sibling, 1 reply; 12+ messages in thread
From: Dmitry A. Kazakov @ 2012-10-22  8:02 UTC (permalink / raw)


On Mon, 22 Oct 2012 09:46:49 +0200, J-P. Rosen wrote:

> Le 22/10/2012 00:55, Yannick Duchêne (Hibou57) a écrit :
>> [Snip]
>> Will this display `A` or `B`? Actually, the program compiled with GNAT
>> 4.6, displays `A`. It displays the same even when the two commented
>> lines are commented‑out and thus the function `F` is declared before the
>> field `F`. I can't remember if the RM have special wordings about it,
>> but I feel this does not honour the principle of least surprise. May be
>> this should be disallowed, to access any one of the two, when both are
>> together visible from a scope?
>> 
> RM2005 4.1.3 (9.2/2): "The designator of the subprogram shall not be the
> same as that of a component of the tagged type visible at the point of
> the selected_component."
> 
> BTW, this corresponds to /my/ view of least surprise: a prefixed view is
> merely a writing simplification, and you are always free not to use it
> without losing any functionality. OTOH, you would have no other way to
> access a subcomponent. Therefore, it makes sense that the subcomponent
> is "stronger".

From a wider (and simpler) point of view a record component is just a pair
of primitive operations (getter/setter) on the record type. That makes them
conflicting with any user-defined primitive operations of same signature.

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



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

* Re: Basic issue with dotted notation and tagged types
  2012-10-21 22:55 Basic issue with dotted notation and tagged types Yannick Duchêne (Hibou57)
  2012-10-22  7:46 ` J-P. Rosen
@ 2012-10-22 10:41 ` AdaMagica
  1 sibling, 0 replies; 12+ messages in thread
From: AdaMagica @ 2012-10-22 10:41 UTC (permalink / raw)


There is a similar problem:

type T is record
  I: Integer;
end record;

function F return T is
  I: Integer := -1;  -- *
begin
  return F.I;  -- recursive call to F and select component I;
               -- or I declared inside F at *
end F;

Without looking in the RM, I guess it's the latter.



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

* Re: Basic issue with dotted notation and tagged types
  2012-10-22  7:46 ` J-P. Rosen
  2012-10-22  8:02   ` Dmitry A. Kazakov
@ 2012-10-22 10:52   ` Marius Amado-Alves
  2012-10-22 11:18     ` Georg Bauhaus
                       ` (2 more replies)
  1 sibling, 3 replies; 12+ messages in thread
From: Marius Amado-Alves @ 2012-10-22 10:52 UTC (permalink / raw)


> RM2005 4.1.3 (9.2/2): "The designator of the subprogram shall not be the
> same as that of a component of the tagged type visible at the point of
> the selected_component."

Hmm... shouldn't the compiler complain then? (about the program above)



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

* Re: Basic issue with dotted notation and tagged types
  2012-10-22 10:52   ` Marius Amado-Alves
@ 2012-10-22 11:18     ` Georg Bauhaus
  2012-10-22 11:37       ` Marius Amado-Alves
  2012-10-22 14:11       ` Yannick Duchêne (Hibou57)
  2012-10-22 11:45     ` Niklas Holsti
  2012-10-22 13:50     ` Yannick Duchêne (Hibou57)
  2 siblings, 2 replies; 12+ messages in thread
From: Georg Bauhaus @ 2012-10-22 11:18 UTC (permalink / raw)


On 22.10.12 12:52, Marius Amado-Alves wrote:
>> RM2005 4.1.3 (9.2/2): "The designator of the subprogram shall not be the
>> same as that of a component of the tagged type visible at the point of
>> the selected_component."
> 
> Hmm... shouldn't the compiler complain then? (about the program above)

Maybe this piece of 2012 support is not yet implemented. With some changes,

with Ada.Text_IO;

    procedure Test2 is

       package IO renames Ada.Text_IO;

       -- type T is tagged;
       -- function F (E : T'Class) return Character;

       package Wrap is

          type T
             is tagged record
             F : Character := 'A';
          end record;

          function F
            (E : T'Class; Dummy : Boolean) -- or E : T; ...
            return T
             is (T'(F => 'B'));

       end wrap;


       E : Wrap.T;
       C : Character := E.F; -- The function or the field?
    begin
       IO.Put (C);
       E := E.F (Dummy => True);
    end;

I get

GNAT GPL 2012 (20120509)
Copyright 1992-2012, Free Software Foundation, Inc.

Compiling: test2.adb (source file time stamp: 2012-10-22 10:31:50)

    29.        E := E.F (Dummy => True);
                     |
        >>> name in call is not a callable entity

 30 lines: 1 error
gnatmake: "test2.adb" compilation error




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

* Re: Basic issue with dotted notation and tagged types
  2012-10-22 11:18     ` Georg Bauhaus
@ 2012-10-22 11:37       ` Marius Amado-Alves
  2012-10-22 14:11       ` Yannick Duchêne (Hibou57)
  1 sibling, 0 replies; 12+ messages in thread
From: Marius Amado-Alves @ 2012-10-22 11:37 UTC (permalink / raw)


> Maybe this piece of 2012 support is not yet implemented...

Dot notation is 2005. But I see now there is a 2012 thing in your code: an expression function. So yes, maybe it's an interference with 2012, which is still a bit "glitchy" in this compiler. The failure to complain about the illegal(?) naming, that is (4.1.3 9.2/2)



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

* Re: Basic issue with dotted notation and tagged types
  2012-10-22 10:52   ` Marius Amado-Alves
  2012-10-22 11:18     ` Georg Bauhaus
@ 2012-10-22 11:45     ` Niklas Holsti
  2012-10-22 14:17       ` Maciej Sobczak
  2012-10-22 13:50     ` Yannick Duchêne (Hibou57)
  2 siblings, 1 reply; 12+ messages in thread
From: Niklas Holsti @ 2012-10-22 11:45 UTC (permalink / raw)


On 12-10-22 13:52 , Marius Amado-Alves wrote:
>> RM2005 4.1.3 (9.2/2): "The designator of the subprogram shall not be the
>> same as that of a component of the tagged type visible at the point of
>> the selected_component."
> 
> Hmm... shouldn't the compiler complain then? (about the program above)

No, the quoted RM part just says that the prefixed call notation
(object.operation) is available /only/ when there is no (visible)
component that has the same name as the operation. If there is a
(visible) component with the same name, then the component effectively
hides the operation, in the object.name syntax.

So the program is legal. However, it may not work as the programmer
intended. An optional compiler warning could be helpful.

This really seems too much of a trap to accept silently. Just for
example, suppose there is a class hierarchy, and Ob is an object of some
derived type, and Foo some primitive operation of this derived type. The
meaning of Ob.Foo then depends on whether or not some level of the
hierarchy adds a component Foo to the type, and whether the component is
visible or not. Yuck! Most cases of confusion will probably be caught by
some type mismatch (or lack of actual parameters), but some will pass
undetected by the compiler.

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



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

* Re: Basic issue with dotted notation and tagged types
  2012-10-22  8:02   ` Dmitry A. Kazakov
@ 2012-10-22 13:46     ` Yannick Duchêne (Hibou57)
  0 siblings, 0 replies; 12+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2012-10-22 13:46 UTC (permalink / raw)


Le Mon, 22 Oct 2012 10:02:16 +0200, Dmitry A. Kazakov  
<mailbox@dmitry-kazakov.de> a écrit:
> From a wider (and simpler) point of view a record component is just a  
> pair of primitive operations (getter/setter) on the record type. That  
> makes them conflicting with any user-defined primitive operations of  
> same signature.

That's why I can't make my mind, as I feel both the same as you and the  
same as J.Pierre too.

J.P Rosen wrote:
> and you are always free not to use it without losing any functionality.
> OTOH, you would have no other way to access a subcomponent.

-- 
“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] 12+ messages in thread

* Re: Basic issue with dotted notation and tagged types
  2012-10-22 10:52   ` Marius Amado-Alves
  2012-10-22 11:18     ` Georg Bauhaus
  2012-10-22 11:45     ` Niklas Holsti
@ 2012-10-22 13:50     ` Yannick Duchêne (Hibou57)
  2 siblings, 0 replies; 12+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2012-10-22 13:50 UTC (permalink / raw)


Le Mon, 22 Oct 2012 12:52:25 +0200, Marius Amado-Alves  
<amado.alves@gmail.com> a écrit:

>> RM2005 4.1.3 (9.2/2): "The designator of the subprogram shall not be the
>> same as that of a component of the tagged type visible at the point of
>> the selected_component."
>
> Hmm... shouldn't the compiler complain then? (about the program above)

No, it's the way it is to be interpreted. If you look at the context what  
J.P. quoted, it's part of a
[Name Resolution  
Rules](http://www.adaic.org/resources/add_content/standards/05rm/html/RM-4-1-3.html)

-- 
“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] 12+ messages in thread

* Re: Basic issue with dotted notation and tagged types
  2012-10-22 11:18     ` Georg Bauhaus
  2012-10-22 11:37       ` Marius Amado-Alves
@ 2012-10-22 14:11       ` Yannick Duchêne (Hibou57)
  1 sibling, 0 replies; 12+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2012-10-22 14:11 UTC (permalink / raw)


Le Mon, 22 Oct 2012 13:18:18 +0200, Georg Bauhaus  
<rm.dash-bauhaus@futureapps.de> a écrit:
> I get
>
> GNAT GPL 2012 (20120509)
> Copyright 1992-2012, Free Software Foundation, Inc.
>
> Compiling: test2.adb (source file time stamp: 2012-10-22 10:31:50)
>
>     29.        E := E.F (Dummy => True);
>                      |
>         >>> name in call is not a callable entity

Looks like it use only the name for the resolution, and does not use the  
signature.

-- 
“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] 12+ messages in thread

* Re: Basic issue with dotted notation and tagged types
  2012-10-22 11:45     ` Niklas Holsti
@ 2012-10-22 14:17       ` Maciej Sobczak
  0 siblings, 0 replies; 12+ messages in thread
From: Maciej Sobczak @ 2012-10-22 14:17 UTC (permalink / raw)


W dniu poniedziałek, 22 października 2012 13:45:29 UTC+2 użytkownik Niklas Holsti napisał:

> Just for
> example, suppose there is a class hierarchy, and Ob is an object of some
> derived type, and Foo some primitive operation of this derived type. The
> meaning of Ob.Foo then depends on whether or not some level of the
> hierarchy adds a component Foo to the type, and whether the component is
> visible or not. Yuck!

I believe this falls in the following description:

http://en.wikipedia.org/wiki/Fragile_base_class

-- 
Maciej Sobczak * http://www.msobczak.com * http://www.inspirel.com



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

end of thread, other threads:[~2012-10-29  2:40 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-21 22:55 Basic issue with dotted notation and tagged types Yannick Duchêne (Hibou57)
2012-10-22  7:46 ` J-P. Rosen
2012-10-22  8:02   ` Dmitry A. Kazakov
2012-10-22 13:46     ` Yannick Duchêne (Hibou57)
2012-10-22 10:52   ` Marius Amado-Alves
2012-10-22 11:18     ` Georg Bauhaus
2012-10-22 11:37       ` Marius Amado-Alves
2012-10-22 14:11       ` Yannick Duchêne (Hibou57)
2012-10-22 11:45     ` Niklas Holsti
2012-10-22 14:17       ` Maciej Sobczak
2012-10-22 13:50     ` Yannick Duchêne (Hibou57)
2012-10-22 10:41 ` AdaMagica

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