From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,262b74f44c7f873e X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news2.volia.net!npeer.de.kpn-eurorings.net!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: prohibit certain generic instantiations in Ada 2005 Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <1139508110.410006.28260@g14g2000cwa.googlegroups.com> <1139515341.782860.197930@f14g2000cwb.googlegroups.com> <1139581110.636535.107910@g44g2000cwa.googlegroups.com> <1139645084.563448.239040@g47g2000cwa.googlegroups.com> <1139734552.205623.152990@o13g2000cwo.googlegroups.com> Date: Sun, 12 Feb 2006 10:49:05 +0100 Message-ID: <4u3pbsvfen5r.9fs4r2tkp6m2.dlg@40tude.net> NNTP-Posting-Date: 12 Feb 2006 10:48:52 MET NNTP-Posting-Host: b8ee0c11.newsread4.arcor-online.net X-Trace: DXC=SSUWCj]N9`=d9772DFAUn2:ejgIfPPld4jW\KbG]kaM88nDPh04j=Q?__RJPB1mA_2mWkfB4cNm_0gT\1QI\6KS?lX[W=iFdHZ> X-Complaints-To: usenet-abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:2865 Date: 2006-02-12T10:48:52+01:00 List-Id: 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