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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,39579ad87542da0e X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,UTF8 X-Received: by 10.180.109.166 with SMTP id ht6mr1646966wib.0.1368583965744; Tue, 14 May 2013 19:12:45 -0700 (PDT) Path: hg5ni110164wib.1!nntp.google.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!85.12.40.131.MISMATCH!xlned.com!feeder3.xlned.com!news.astraweb.com!border5.a.newsrouter.astraweb.com!border2.nntp.ams.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeed.news.ucla.edu!nrc-news.nrc.ca!News.Dal.Ca!citadel.nobulus.com!goblin3!goblin2!goblin.stu.neva.ru!aioe.org!.POSTED!not-for-mail From: =?utf-8?Q?Yannick_Duch=C3=AAne_=28Hibou57?= =?utf-8?Q?=29?= Newsgroups: comp.lang.ada Subject: Re: Seeking for papers about tagged types vs access to subprograms Date: Sun, 12 May 2013 08:44:13 +0200 Organization: Ada @ Home Message-ID: References: <1vrhb7oc4qbob$.q02vuouyovp5$.dlg@40tude.net> <19lrzzbgm77v6.1dzpgqckptaj6.dlg@40tude.net> <1bfhq7jo34xpi.p8n2vq6yjsea.dlg@40tude.net> <12gn9wvv1gwfk.10ikfju4rzmnj.dlg@40tude.net> NNTP-Posting-Host: 2WDySVo3ljzMoJ+IkbOqwg.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.15 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes Content-Transfer-Encoding: Quoted-Printable Date: 2013-05-12T08:44:13+02:00 List-Id: Le Sat, 11 May 2013 23:06:25 +0200, Niklas Holsti = a =C3=A9crit: > type File_State is (Is_Closed, Is_Open); > > type File_Object (State : File_State :=3D Is_Closed) is > record > case State is > when Is_Closed =3D> null; > when Is_Open =3D> Handle : System.IO.File_Handle; > end case; > end record; > > subtype Closed_File is File_Object (State =3D> Is_Closed); > subtype Open_File is File_Object (State =3D> Is_Open ); > > By using subtypes on formal parameters, we can indicate that the > available operations on a File_Object depend on the actual subtype > (i.e. the state), except for one deficiency, on which more below. > > First, reading and writing is possible only for open files: > > procedure Read (File : in Open_File; ...) > procedure Write (File : in Open_File; ...) > > Second, a Closed file can be Opened, and an Open file can be Closed: > > procedure Open (File : in out Closed_File); > procedure Close (File : in out Open_File ); > > The problem here is that these operations should change the subtype of= > the "in out" parameter: Open changes the File from Closed_File to > Open_File, and Close changes it from Open_File to Closed_File. Additionally to the idea you suggested, that may also suggest to use a = function instead of a procedure with in/out parameter, with a new = declaration for each of the important state changes which would become = clearly visible (still what if this already was a function already = returning something=E2=80=A6 Ada functions can return only one single el= ement). = There is now the question about compilers: are compilers clever enough t= o = statically catch subtype miss=E2=80=91match in such a use cases and give= warnings = (I believe no), and are compilers clever enough to optimize out multiple= = object declaration into a single one, changing its subtype (I believe no= , = too), while the latter is not an issue if you use a default discriminant= = as you did. Another issue with subtype, is that it requires the `File_Object` full = definition to be public, or else, there is no way to define a subtype of= = it as the example do (no opaque type). The idea is still nice and worth to investigate. -- = =E2=80=9CSyntactic sugar causes cancer of the semi-colons.=E2=80=9D [1] =E2=80=9CStructured Programming supports the law of the excluded muddle.= =E2=80=9D [1] [1]: Epigrams on Programming =E2=80=94 Alan J. =E2=80=94 P. Yale Univers= ity