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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,cc4f25d878383cc X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-12-06 11:52:12 PST Path: archiver1.google.com!news1.google.com!postnews1.google.com!not-for-mail From: britt@adapower.net (Britt Snodgrass) Newsgroups: comp.lang.ada Subject: Re: Dimensionality Checking (Ada 20XX) Date: 6 Dec 2001 11:52:12 -0800 Organization: http://groups.google.com/ Message-ID: <36c6f8dd.0112061152.333c9de@posting.google.com> References: <3C0A5054.E74A82E7@worldnet.att.net> <9ugs4v$8d7lj$1@ID-25716.news.dfncis.de> <3c0cc931.16965562@News.CIS.DFN.DE> NNTP-Posting-Host: 216.253.238.30 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1007668332 15973 127.0.0.1 (6 Dec 2001 19:52:12 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 6 Dec 2001 19:52:12 GMT Xref: archiver1.google.com comp.lang.ada:17528 Date: 2001-12-06T19:52:12+00:00 List-Id: Perhaps this is not a new proposal but it seems to me that units (dimensionality) in Ada code would be most naturally implemented as attributes of objects or types. The units of an object or type would be specified with a representation clause. If unspecified, the unit attribute would default to "null" (no units). The compiler could then enforce unit consistency during compilation or generate runtime unit checking code. For example: with Ada.Text_IO; with Ada.Dimensionality.ISO_Metric_Units; -- predefined for kilograms, meters, etc. with Ada.Dimensionality.My_User_Defined_Units -- parsecs_per_picosecond, etc. use Ada.Dimensionality; package body Proposed_Unit_Syntax_Example is type Magnitude is digits 15; Delta_Position : Magnitude; for Delta_Position'Unit use ISO_Metric_Units.Meter; Delta_Time : Magnitude; for Delta_Time'Unit use ISO_Metric_Units.Second; Current_Speed : Magnitude; for Current_Speed'Unit use ISO_Metric_Units.Meter / ISO_Metric_Units.Second; Accel : Magnitude; for Accel'Unit use ISO_Metric_Units.Meter / ISO_Metric_Units.Second**2; -- note use of operators in attribute definition. begin Delta_Position := 500.0; -- meters implied by unit attribute Delta_Interval := 10.0; -- seconds implied by unit attribute Current_Speed := Delta_Position / Delta_Interval; -- OK, units match Current_Speed := Delta_Position / Delta_Interval**2; -- won't compile or raises a predefined Unit_Error exception; exception when Unit_Error => Ada.Text_IO.Put_Line ("Oops! I just passed Mars without stopping to orbit!"); end Proposed_Unit_Syntax_Example; Since all of the variables in this example are of the same type, no new arithmetic operators need to be defined. However the compiler would use the unit attributes to ensure that any resulting right-side composite unit matches the left-side unit before allowing an assignment to proceed. Most unit inconsistencies would be caught during compilation. If a unit mismatch can't be caught until runtime, then a predefined Unit_Error exception would be raised (unless the code had been compiled with Pragma Suppress (Unit_Checks) ). Britt Snodgrass