comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: prohibit certain generic instantiations in Ada 2005
Date: Sun, 12 Feb 2006 10:49:05 +0100
Date: 2006-02-12T10:48:52+01:00	[thread overview]
Message-ID: <4u3pbsvfen5r.9fs4r2tkp6m2.dlg@40tude.net> (raw)
In-Reply-To: 1139734552.205623.152990@o13g2000cwo.googlegroups.com

On 12 Feb 2006 00:55:52 -0800, matteo.bordin@gmail.com wrote:

> As I explained in a prewieus post, my int/float example was just
> didactic. I want that kind of checking because I want to use generics
> as a generative environment for a component-based approach. If I can
> map to the source code the knowledge that some template instantiations
> are not allowed, then I can let the compiler check for me if the user
> of the library I am writing is doing something wrong. For example,
> imagine I want to compose a car component with an Engine and a
> Transmission. Electric engine can be coupled only with automatic
> transmission, while gasoline engine can work with both automatic and
> manual transmission. Then the Car component is:
> 
> generic
>     type My_Engine is new Engine with private;
>     type My_Transmission is new Transmission with private;
> package Car is
> ....
> 
> Then I want to prohibit:
> 
> package My_Car is new Car(My_Engine => Electric_Engine, My_Transmission
> => Manual_Tansmission).
> 
> As you can see there is a reason to do that. And there is other reasons
> to want static (compile-time) checking insteam of run-time exceptions.

OK, but there must be something that requires automatic transmission for an
electric engine. This something need to be factored out and specified in
the contract. It is an OOA/D question.

Consider an implementation of Engine. If My_Engine cannot be controlled by
Manual_Transmission, then, say,

   procedure Switch (E : in out Engine; T : Transmission'Class);

will raise run-time error. So the problem is not in Car, but between Engine
and Transmission. It is not Car, which imposes constraints.

OK, you can still do it in Car, if you want:

generic
   type My_Engine is new Engine with private;
   type My_Transmission is new Transmission with private;
   with procedure Switch (E : in out My_Engine; T : My_Transmission) is <>;
package Car is

Now, if there is no Switch for Electric_Engine and Manual_Tansmission, Car
will not be instantiated.

However, I see it as a bad design, because the relationship between Engine
and Transmission is not clearly specified. But even so, it is not obvious
why Car need to be generic rather than mix-in:

   type Abstract_Car
      (E : access Engine'Class; T : access Transmission'Class) is ...

More specialized cars could be defined using inheritance:

   type Abstract_Electric_Car
      (E : Electric_Engine'Class; T : Electric_Transmission'Class) is
         new Abstract_Car (E, T) with ...

[my rule of thumb: avoid generics]

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



  reply	other threads:[~2006-02-12  9:49 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-09 18:01 prohibit certain generic instantiations in Ada 2005 matteo.bordin
2006-02-09 20:02 ` Matthew Heaney
2006-02-10 14:18   ` matteo.bordin
2006-02-10 18:25     ` Dmitry A. Kazakov
2006-02-10 19:03       ` Georg Bauhaus
2006-02-11 10:45         ` Dmitry A. Kazakov
2006-02-11  8:04       ` matteo.bordin
2006-02-11  9:54         ` Martin Krischik
2006-02-11 10:20           ` matteo.bordin
2006-02-11 12:46             ` Martin Krischik
2006-02-11 10:45         ` Dmitry A. Kazakov
2006-02-11 22:20           ` Jeffrey R. Carter
2006-02-11 15:36         ` Stephen Leake
2006-02-12  8:55           ` matteo.bordin
2006-02-12  9:49             ` Dmitry A. Kazakov [this message]
2006-02-12 12:41               ` matteo.bordin
2006-02-13 19:14             ` Matthew Heaney
replies disabled

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