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-07 01:37:18 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!tar-alcarin.cbb-automation.DE!not-for-mail From: dmitry@elros.cbb-automation.de (Dmitry A. Kazakov) Newsgroups: comp.lang.ada Subject: Re: Dimensionality Checking (Ada 20XX) Date: Fri, 07 Dec 2001 09:37:16 GMT Message-ID: <3c108b58.4453515@News.CIS.DFN.DE> References: <3C0A5054.E74A82E7@worldnet.att.net> <9ugs4v$8d7lj$1@ID-25716.news.dfncis.de> <3c0cc931.16965562@News.CIS.DFN.DE> <36c6f8dd.0112061152.333c9de@posting.google.com> NNTP-Posting-Host: tar-alcarin.cbb-automation.de (212.79.194.111) X-Trace: fu-berlin.de 1007717836 10638333 212.79.194.111 (16 [77047]) X-Newsreader: Forte Free Agent 1.21/32.243 Xref: archiver1.google.com comp.lang.ada:17554 Date: 2001-12-07T09:37:16+00:00 List-Id: On 6 Dec 2001 11:52:12 -0800, britt@adapower.net (Britt Snodgrass) wrote: >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) ). I would also prefer a representation clause over rather obscure pragmas, but the question stands: how to do class-wide unit programming? For instance, how to write [non-generic] subprogram IntegrateTime which takes /s and returns ? How to write a subprogram that gets user input from a dialog field and returns some dimensioned value? Therefore I would prefer a solution based on discriminants or else tags provided that the compiler will remove all extra data fields and run-time checks in case when units are statically known. Regards, Dmitry Kazakov