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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c406e0c4a6eb74ed X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews2.google.com!not-for-mail From: kevin.cline@gmail.com (Kevin Cline) Newsgroups: comp.lang.ada Subject: Re: ADA Popularity Discussion Request Date: 8 Sep 2004 18:08:40 -0700 Organization: http://groups.google.com Message-ID: References: <49dc98cf.0408110556.18ae7df@posting.google.com> <6F2Yc.848$8d1.621@newsread2.news.pas.earthlink.net> <413e2fbd$0$30586$626a14ce@news.free.fr> NNTP-Posting-Host: 24.219.97.214 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1094692120 12787 127.0.0.1 (9 Sep 2004 01:08:40 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 9 Sep 2004 01:08:40 +0000 (UTC) Xref: g2news1.google.com comp.lang.ada:3520 Date: 2004-09-08T18:08:40-07:00 List-Id: Lionel Draghi wrote in message news:<413e2fbd$0$30586$626a14ce@news.free.fr>... > jayessay wrote: > ... > > Exactly. Actually this sort of development will save you much more > > time and money than you could ever hope for from typical static typing. > > How could this be? > With powerful typing you write code. > Without, you write as much code and much more tests. I code test-first, and the amount of test code is relatively independent of the language. > > Let's compare: > > A : > function Random_Index return Integer; > -- return's a value between 1 and 10 > > B : > type Index is range 1 .. 10; > function Random_Index return Index; > > If considering code and comments, both are of the same length. The first version leaves me wondering if the comment is correct. The second version leaves me wondering if it sometimes throws Constraint_Error. There's no way to verify that A always returns a value from 1 to 10 or to verify that B never throws Constraint_Error without inspecting the function body. I'm also wondering what happens when you write Random_Index + Random_Index ? Or Random_Index * Random_Index ? > On the other hand, in the A version, I need to check the return value on > each call. That's silly. You have to trust functions to satisfy their postconditions. Even in Ada relatively few post-conditions can be handled by type checks. Would you check after every call to Create_Linked_List to make sure that you got an empty list? And then check after every call to Insert_At_Head to make sure that the list hadn't somehow become circular? If you want bounded types in C++ it's easy enough to write a template class: template class bounded { private: int _value; public: operator int { return _value; } explicit bounded(int value): _value(value) { if (value < lower_bound || value > upper_bound) { throw(...); } operator=(int value) { *this = bounded(value); } } } Then your function is: bounded<1,10> random_index() {...} If the spirit moves you, you can even write: template bounded operator+(bounded b2) { return _value + b2.value; } Now adding bounded<1,10> to bounded<0,5> gives a result of the proper type: bounded<1,15>. In LISP-y languages, you would just write something like: (defun random-index () (bound 1 10 ... )