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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1dd28d5040ded1f8 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-05-16 12:40:55 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!nntp.abs.net!uunet!dca.uu.net!ash.uu.net!spool0900.news.uu.net!reader0900.news.uu.net!not-for-mail Message-ID: <3CE40B8E.70102@mail.com> Date: Thu, 16 May 2002 15:42:06 -0400 From: Hyman Rosen User-Agent: Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.0rc2) Gecko/20020510 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Announce: Grace project site operational References: <4519e058.0205140718.3ddb1b2a@posting.google.com> <3CE15D0A.3050100@mail.com> <2s44eu0fm4g6606h9p4stb1b5oc0nmg5u8@4ax.com> <3CE2A946.5030808@mail.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Organization: KBC Financial Products Cache-Post-Path: master.nyc.kbcfp.com!unknown@mosquito.nyc.kbcfp.com X-Cache: nntpcache 2.3.3 (see http://www.nntpcache.org/) NNTP-Posting-Host: 204.253.250.10 X-Trace: 1021578053 reader0.ash.ops.us.uu.net 6548 204.253.250.10 Xref: archiver1.google.com comp.lang.ada:24228 Date: 2002-05-16T15:42:06-04:00 List-Id: Dmitry A. Kazakov wrote: > In short, generics are macros. In exactly the same sense that FOR loops are GOTOs. > Generics are bad for units because they enforce dimensioned > values be of unrelated types. Why is that bad? > For instance, a discriminant-based variant: > type Measure (SI : Unit := Unitless) is record > Gain : Number; > end record; In all Ada versions that anyone here has ever mentioned, this drags along a discriminant as part of the object, making it bigger. > X := Y; -- Constraint_Error > > A decent Ada compiler should give you warning at compile time. In the type-based version, it *must* fail at compile time. > How do you write a Put for dimensioned values? It shall be class-wide > = work on all unit [sub]types. Do you want to make it generic and > instantiate each time? Yes, I would make it generic and instantiate each time. In C++, of course, the instantiation would be done for me. Here's what it would look like for a simple gram-centimeter-second system. All of the 'if' tests in the code below are static at compile time, so they will not consume any runtime. template Stream &operator<<(Stream &out, const Unit &u) { out << u.value; if (Mass > 0) out << " gm"; if (Mass > 1) out << "^" << Mass; if (Distance > 0) out << " cm"; if (Distance > 1) out << "^" << Distance; if (Time > 0) out << " sec"; if (Time > 1) out << "^" << Time; if (Mass < 0 || Distance < 0 || Time < 0) out << " /"; if (Mass < 0) out << " gm"; if (Mass < -1) out << "^" << -Mass; if (Distance < 0) out << " cm"; if (Distance < -1) out << "^" << -Distance; if (Time < 0) out << " sec"; if (Time < -1) out << "^" << -Time; return out; } And it's used like this, given suitable typedefs and values: Mass m = 3 * pound; Acceleration g = 32 * foot / second / second std::cout << " Mass = " << m << " Acceleration = " << g << " Force = " << m * g << '\n'; > There is a roller dynamometer. Shrug. Yes, if you really have a system where everything is completely dynamic, then a type-based solution will be difficult or perhaps impossible to use. On the other hand, I'm guessing that most people's sensors stay put and measure one thing.