comp.lang.ada
 help / color / mirror / Atom feed
* Overring function and its returned type
@ 2012-11-09  7:13 Yannick Duchêne (Hibou57)
  2012-11-09  7:22 ` Yannick Duchêne (Hibou57)
                   ` (4 more replies)
  0 siblings, 5 replies; 29+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2012-11-09  7:13 UTC (permalink / raw)


I feel a bit disappointed with a construct I never attempted before, and  
which seems to not be valid according to GNAT, which I believe is not  
wrong, as I can't recall to have read it's allowed. First a snippet, then  
some comments.


     procedure Ex
     is

        -- ========================================

        package P1 is

           type R is interface;
           type T is interface;

           not overriding
           function F
             (E : T)
              return R'Class
              is abstract;
        end;

        -- ========================================

        package P2 is

           type R is interface and P1.R;
           type T is interface and P1.T;

           overriding
           function F
             (E : T)
              return R'Class -- Not overriding :-(
              is abstract;
        end;

        -- ========================================

        package P3 is

           type R is new P1.R with null record;
           type T is new P1.T with null record;

           overriding
           function F
             (E : T)
              return P1.R'Class;
        end;

        -- ----------------------------------------

        package body P3 is

           overriding
           function F
             (E : T)
              return P1.R'Class
           is
              X : R;
           begin
              return X; -- This is OK
           end;
        end;

        -- ========================================

     begin
        null;
     end Ex;


In `P2`, `R` inherits from `P1.R`, so belongs to `P1.R'Class`, and is a  
value returned type for an concrete function `F`, as `P3` shows. But GNAT  
reject it, complaining `P2.F` is not overriding (by the way, the example  
also shows how much it is safer to always add an “[not] overriding”  
qualification to dispatching subprograms).

As `P2.R'Class` is compatible with `P1.R'Class`, why not allow this for an  
overriding function? This case makes me think of the one fixed by Ada  
2012, about the returned class wide type of a function and the extended  
returned statement, explained in [Extended return  
statements](http://www.ada-auth.org/standards/12rat/html/Rat12-4-6.html).

If that's not allowed with good reasons, then what are these?

A work around could be to use overloading instead of overriding, but that  
may cause troubles when there is no other arguments to the function, in  
which case this will probably looks ambiguous in some contexts. This can  
also cause issues when there are predicates using `F` in `P1`. I could  
give a tiny example of such a case if someone need one to see why it may  
be an issue.

If that could be OK to allow overriding functions to change their return  
type to a compatible type when this type is class wide, then is there a  
chance to add it as a last minute change for Ada 2012?


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

end of thread, other threads:[~2012-11-22  3:25 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-09  7:13 Overring function and its returned type Yannick Duchêne (Hibou57)
2012-11-09  7:22 ` Yannick Duchêne (Hibou57)
2012-11-09  8:24 ` Dmitry A. Kazakov
2012-11-09  9:14   ` Yannick Duchêne (Hibou57)
2012-11-09 13:11     ` Dmitry A. Kazakov
2012-11-09 21:36       ` Yannick Duchêne (Hibou57)
2012-11-09 19:24   ` Adam Beneschan
2012-11-09 19:34 ` Adam Beneschan
2012-11-09 22:00   ` J-P. Rosen
2012-11-09 22:30     ` Yannick Duchêne (Hibou57)
2012-11-09 19:57 ` sbelmont700
2012-11-09 21:10   ` Yannick Duchêne (Hibou57)
2012-11-09 21:56     ` sbelmont700
2012-11-10  0:28     ` Yannick Duchêne (Hibou57)
2012-11-10  2:35       ` Yannick Duchêne (Hibou57)
2012-11-15 15:13   ` Peter C. Chapin
2012-11-16 10:40     ` Maciej Sobczak
2012-11-16 12:39       ` Peter C. Chapin
2012-11-16 15:27         ` Maciej Sobczak
2012-11-16 17:29           ` Peter C. Chapin
2012-11-17  4:16           ` Yannick Duchêne (Hibou57)
2012-11-17 19:11             ` Robert A Duff
2012-11-18 14:53               ` AdaMagica
2012-11-19  8:41                 ` Yannick Duchêne (Hibou57)
2012-11-19 13:04                   ` AdaMagica
2012-11-19 15:19                     ` Dmitry A. Kazakov
2012-11-19 23:42                   ` Randy Brukardt
2012-11-10  7:55 ` Randy Brukardt
2012-11-11  1:02   ` 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