comp.lang.ada
 help / color / mirror / Atom feed
From: "Peter C. Chapin" <pcc482719@gmail.com>
Subject: Re: Overring function and its returned type
Date: Thu, 15 Nov 2012 10:13:05 -0500
Date: 2012-11-15T10:13:05-05:00	[thread overview]
Message-ID: <cYydnSvuUM0cmzjNRVn_vwA@giganews.com> (raw)
In-Reply-To: <97a6946f-a707-4dd3-872b-9e851fcf9462@googlegroups.com>

On 11/09/2012 02:57 PM, sbelmont700@gmail.com wrote:

> More generally, you are talking about 'covariant return types', which
> is a feature only a handful of languages support (notably C++).
> IMHO, i don't really see the need for it; a type is either a child
> (and matches the parent, as in P3) or it doesn't and it's not (as in
> P2).

I realize this thread is dead but I found it interesting and looked into 
the topic a little. It is true that C++ supports covariant refinement of 
return types as does, for example, Scala. In theory it should also be 
type safe to allow contravariant refinement of parameter types. Scala 
does not allow that... which surprised me considering Scala's reputation 
of having a very expressive type system.

I talked with some people in the Scala community and it turns out the 
reason for the restriction is related to overload resolution and 
ambiguity. In the case of C++ there is no overloading on return types 
and thus allowing covariant refinement of return types is straight 
forward there. However, since Ada does allow overloading on return 
types, complications arise.

To illustrate let me start with a simple derivation class:

package Vehicles is
   type Vehicle is interface;
   type Car is abstract new Vehicle with null record;
   type Sports_Car is abstract new Car with null record;
end Vehicles;

Now consider:

with Vehicles;

package Animals is
   type Animal is interface;
   function F(A : Animal) return Vehicles.Vehicle'Class is abstract;
   function F(A : Animal) return Vehicles.Car'Class is abstract;

   type Cat is abstract new Animal with null record;
   overriding function F(C: Cat) return Vehicles.Sports_Car'Class is 
abstract;
end Animals;


Suppose covariant refinement of return types was allowed in Ada. In that 
case which F is being overridden here? Since Sports_Car <: Vehicle and 
also Sports_Car <: Car the overriding is ambiguous.

I suppose rules could be written to clarify these things but it probably 
wouldn't be worth the additional language complexity.

Peter




  parent reply	other threads:[~2012-11-15 15:13 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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)
replies disabled

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