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 12:55:24 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!arclight.uoregon.edu!wn4feed!worldnet.att.net!204.127.198.204!attbi_feed4!attbi.com!rwcrnsc54.POSTED!not-for-mail From: "Mark Lundquist" Newsgroups: comp.lang.ada 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> Subject: Re: Dimensionality Checking (Ada 20XX) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Message-ID: <%WQP7.2224$L51.15607@rwcrnsc54> NNTP-Posting-Host: OIQP7-15629-L51-15127@rwcrnsc54 X-Complaints-To: abuse@attbi.com X-Trace: rwcrnsc54 1007672123 OIQP7-15629-L51-15127@rwcrnsc54 (Thu, 06 Dec 2001 20:55:23 GMT) NNTP-Posting-Date: Thu, 06 Dec 2001 20:55:23 GMT Organization: AT&T Broadband Date: Thu, 06 Dec 2001 20:55:23 GMT Xref: archiver1.google.com comp.lang.ada:17534 Date: 2001-12-06T20:55:23+00:00 List-Id: In my post, I mentioned the idea of specifying units and their relationships independent of types, then somehow "binding" units to types. At first glance, your idea seems promising for the "binding" part. Very nice. I hadn't thought of using attributes for this, and as you say it is a natural notation for Ada. -- mark "Britt Snodgrass" wrote in message news:36c6f8dd.0112061152.333c9de@posting.google.com... > 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