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=unavailable autolearn_force=no version=3.4.4 Path: border2.nntp.dca1.giganews.com!nntp.giganews.com!news.glorb.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post02.iad.highwinds-media.com!fx20.iad.POSTED!not-for-mail From: Brad Moore User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: How to use read-only variables? References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 68.145.219.148 X-Complaints-To: internet.abuse@sjrb.ca X-Trace: 1420404339 68.145.219.148 (Sun, 04 Jan 2015 20:45:39 UTC) NNTP-Posting-Date: Sun, 04 Jan 2015 20:45:39 UTC Date: Sun, 04 Jan 2015 13:45:39 -0700 X-Received-Bytes: 4784 X-Received-Body-CRC: 1016581393 Xref: number.nntp.giganews.com comp.lang.ada:191650 Date: 2015-01-04T13:45:39-07:00 List-Id: > Fourth, in what practical cases do you really use 'access const' for > read-only variables; I thought my case a pretty typical one? > I generally try to avoid using access types and access values if at all possible, especially in a visible part of a package, since that means the client has to deal with them. If I need to use them, I try to hide all use in the private part of the package, or in the body. I find it makes better abstractions when you do that, that are less error prone to use by the client. I might use an access const internally in a package when there are multiple interconnected data objects, and if some of those comments are static or should not be modified within the abstraction. eg... package P is type Body_Names is (Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto); subtype Planet_Names is Body_Names range Mercury .. Body_Names'Last; subtype Kilograms is Long_Long_Float; subtype Body_Mass is Kilograms; function Get_Mass (Body_Name : Body_Names) return Body_Mass; function Moon_Count (Planet_Name : Planet_Names) return Natural; function Moon_Mass (Planet_Name : Planet_Names; Moon_Number : Natural) return Body_Mass; end P; package body P is type Star (Name_Length : Positive) is record Name : String (1 .. Name_Length); Mass : Body_Mass; end record; type Moon_Mass_Array is array (Positive range <>) of Body_Mass; type Planet (Name : Body_Names; Moons : Natural) is record Moon_Masses : Moon_Mass_Array (1 .. Moons); Mass : Body_Mass; end record; type Planet_Array is array (Planet_Names) of access constant Planet; type Solar_System is record The_Sun : Star (Name_Length => Body_Names'Image (Sun)'Length); Planets : Planet_Array; end record; Planet_Mercury : aliased constant Planet := (Name => Mercury, Moons => 0, Moon_Masses => <>, Mass => 3.3022E+23); Planet_Venus : aliased constant Planet := (Name => Venus, Moons => 0, Moon_Masses => <>, Mass => 4.8676E+24); Planet_Earth : aliased constant Planet := (Name => Earth, Moons => 1, Moon_Masses => (1 => 7.3477E+22), Mass => 5.97219E+24); ... Our_Solar_System : constant Solar_System := (The_Sun => (Name_Length => Body_Names'Image (Sun)'Length, Name => Body_Names'Image (Sun), Mass => 1.989E+1030), Planets => (Mercury => Planet_Mercury'Access, Venus => Planet_Venus'Access, Earth => Planet_Earth'Access, ... )); function Get_Mass (Body_Name : Body_Names) return Body_Mass is begin return (if Body_Name = Sun then Our_Solar_System.The_Sun.Mass else Our_Solar_System.Planets (Body_Name).Mass); end Get_Mass; function Moon_Count (Planet_Name : Planet_Names) return Natural is (Our_Solar_System.Planets (Body_Name).Moons); function Moon_Mass (Planet_Name : Planet_Names; Moon_Number : Natural) return Body_Mass is (Our_Solar_System.Planets (Planet_Name).Moon_Masses (Moon_Number)); end P; Here all the data structures are hidden in the body. The client of package P does not even have to be aware of how the objects are interconnected, or that access values are being used. I used access constant here because the data for the planets is static and cannot be modified. I used access types because the planet objects are variable in size and cannot be declared in an array, however an array of access values is possible. Brad