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,d89b08801f2aacae X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-04-30 12:28:59 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!skynet.be!skynet.be!newsfeed.online.be!newsfeed1.uni2.dk!news.net.uni-c.dk!uninett.no!ntnu.no!not-for-mail From: Preben Randhol Newsgroups: comp.lang.ada Subject: Re: Is strong typing worth the cost? Date: Tue, 30 Apr 2002 19:28:57 +0000 (UTC) Organization: Norwegian university of science and technology Message-ID: References: <4519e058.0204290722.2189008@posting.google.com> <3CCE8523.6F2E721C@earthlink.net> <3CCEB246.9090009@worldnet.att.net> NNTP-Posting-Host: kiuk0156.chembio.ntnu.no X-Trace: tyfon.itea.ntnu.no 1020194937 13132 129.241.83.82 (30 Apr 2002 19:28:57 GMT) X-Complaints-To: usenet@itea.ntnu.no NNTP-Posting-Date: Tue, 30 Apr 2002 19:28:57 +0000 (UTC) User-Agent: slrn/0.9.7.4 (Linux) Xref: archiver1.google.com comp.lang.ada:23292 Date: 2002-04-30T19:28:57+00:00 List-Id: On Tue, 30 Apr 2002 19:01:58 +0000 (UTC), Preben Randhol wrote: > > At least one cost/benefit of strong typing is that you save a lot of > time chasing odd bugs with your debugger. The compiler has a lot more > information to go on in order to track down bugs. Therefore it will be > able to tell you about them when you compile. Example (though it is trivial example): --------------------------------------------------------- -- Weak typed version: --------------------------------------------------------- with Ada.Text_IO; use Ada.Text_IO; procedure weak is Peter_Weight : Integer := 75; Peter_Height : Integer := 180; Ann_Weight : Integer := 55; Ann_Height : Integer := 165; Total_Height : Integer; Total_Weight : Integer; begin Total_Height := Peter_Height + Ann_Weight; Total_Weight := Peter_Weight + Ann_Weight; Put_Line ("Total height is: " & Integer'Image(Total_Height)); Put_Line ("Total weight is: " & Integer'Image(Total_Weight)); if Peter_Weight > Ann_Height then Put_Line ("Peter is heavier than Ann"); else Put_Line ("Ann is heavier than Peter"); end if; end weak; --------------------------------------------------------- running gnatmake on weak.adb goes without any complaints and when running the program the result is: Total height is: 235 Total weight is: 130 Ann is heavier than Peter Hmm something doesn't seem right with that output. Let's debug. Now let's do this a bit more strongly typed: --------------------------------------------------------- -- Strong typed version: --------------------------------------------------------- with Ada.Text_IO; use Ada.Text_IO; procedure Strong is type Weight_Type is range 0..500; -- kg type Height_Type is range 0..200; -- cm Peter_Weight : Weight_Type := 75; Peter_Height : Height_Type := 180; Ann_Weight : Weight_Type := 55; Ann_Height : Height_Type := 165; Total_Height : Height_Type; Total_Weight : Weight_Type; begin Total_Height := Peter_Height + Ann_Weight; Total_Weight := Peter_Weight + Ann_Weight; Put_Line ("Total height is: " & Height_Type'Image(Total_Height)); Put_Line ("Total weight is: " & Weight_Type'Image(Total_Weight)); if Peter_Weight > Ann_Height then Put_Line ("Peter is heavier than Ann"); else Put_Line ("Ann is heavier than Peter"); end if; end strong; --------------------------------------------------------- Running gnatmake on strong.adb gives: gnatmake strong.adb gnatgcc -c strong.adb strong.adb:17:33: invalid operand types for operator "+" strong.adb:17:33: left operand has type "Height_Type" defined at line 6 strong.adb:17:33: right operand has type "Weight_Type" defined at line 5 strong.adb:23:20: invalid operand types for operator ">" strong.adb:23:20: left operand has type "Weight_Type" defined at line 5 strong.adb:23:20: right operand has type "Height_Type" defined at line 6 gnatmake: "strong.adb" compilation error Oops I seem to have some errors in my program *BLUSH* ;-) OK this is very trivial, but think about this in a big complex program where the variables are not so obvious. Say a Mars probe that crashes due to a simple bug or whatever. Preben