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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,50137bb64a119cfc X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-02-15 15:44:37 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!sjc70.webusenet.com!news.webusenet.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!harp.news.atl.earthlink.net!not-for-mail From: Richard Riehle Newsgroups: comp.lang.ada Subject: Re: "access constant" discriminant Date: Sat, 15 Feb 2003 15:53:32 -0800 Organization: AdaWorks Software Engineering Message-ID: <3E4ED2FC.3310564A@adaworks.com> References: <_TO1a.14664$9y2.6601@nwrddc01.gnilink.net> <3CS1a.55972$2H6.1357@sccrnsc04> <3E4E9248.3E71D984@adaworks.com> Reply-To: richard@adaworks.com NNTP-Posting-Host: 3f.bb.88.01 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Server-Date: 15 Feb 2003 23:45:00 GMT X-Mailer: Mozilla 4.7 [en] (Win98; I) X-Accept-Language: en Xref: archiver1.google.com comp.lang.ada:34138 Date: 2003-02-15T23:45:00+00:00 List-Id: Larry Kilgallen wrote: > Why is it necessary to have the parameter be an access type ? Good question. Consider this package, package Side_Effect_Experiment is type Item_Type is private; type Reference is access all Item_Type; procedure Create (Data : in out Item_Type); function Is_Valid (Data : in Item_Type) return Boolean; function Is_Valid (Data : in Reference) return Boolean; function Is_Valid (Data : access Item_Type) return Boolean; private type Item_Type is record ID : Natural := 0; Description : String(1..20) := (others => ' '); Valid : Boolean := False; end record; end Side_Effect_Experiment; with this corresponding package body, package body Side_Effect_Experiment is procedure Create (Data : in out Item_Type) is begin null; end Create; function Is_Valid (Data : in Item_Type) return Boolean is begin Data.Valid := True; -- not valid for in parameter return Data.Valid; end Is_Valid; function Is_Valid (Data : in Reference) return Boolean is begin Data.Valid := True; return Data.Valid; end Is_Valid; function Is_Valid (Data : access Item_Type) return Boolean is begin Data.Valid := True; return Data.Valid; end Is_Valid; end Side_Effect_Experiment; Note that the side-effect cannot happen when we have a normal in parameter. However, it can occur with any variation of an access type parameter. By adding syntax to the function such as, constant function Is_Valid(Data : access Item_Type) return Boolean; or function Is_Valid (Data : access constant Item_Type) return Boolean; the compiler would be able to note the attempt at a side-effect and prevent it. Richard Riehle