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: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Comparing Access Types Date: Thu, 9 Nov 2017 09:33:49 +0100 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: lKHBldubgAWx1EqbQpQ5LQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.8.2 Xref: feeder.eternal-september.org comp.lang.ada:48779 Date: 2017-11-09T09:33:49+01:00 List-Id: On 09/11/2017 06:37, Jere wrote: > I have a package which uses a type to wrap an access type. I thought it > might be nice to provide the basic comparison functions, "=", "<", ">", etc. > but access types can only be compared using "=" and "/=". I figured that > way, my types could be used in some sort of sorted container, like a map. > > I want to keep the incomplete type for other reasons, so that seems to limit > my options. > > 1. Address_To_Access_Conversions is out (cannot use an incomplete type) > 2. I cannot simply do var.all'Address (Again, incomplete type) Of course you can. > Is there some way to do this within the Ada standard or am I hamstrung > due to the incomplete type specification? > > Also, why weren't <, >, <=, >= provided for access types. Even if the > representation of an Access type is implementation defined, surely > those operators could have been defined. No, in general case it cannot, in the spirit of the semantics that A < B if the physical machine address of A < B. Actually even equality cannot be for segmented memory but it felt good enough. > A toned down example: > > generic > type Item_Type(<>); > type Item_Access is access Item_Type; > package My_Package is > > type Some_Type is record > Reference : Item_Access := null; > end record; > > function "<" (Left,Right : Some_Type) return Boolean; > > end My_Package; > > package body My_Package is > > function "<" (Left,Right : Some_Type) return Boolean is > begin > return Left.Reference < Right.Reference; --fails to compile > end "<"; You have two options: 1. Compare addresses, e.g. with System; use System; function "<" (Left,Right : Some_Type) return Boolean is begin return ( Right.Reference /= null and then ( Left.Reference = null or else Left.Reference.all'Address < Right.Reference.all'Address ) ); end "<"; 2. A better way is to use the native object's order: generic type Item_Type(<>); type Item_Access is access Item_Type; with function "=" (Left, Right : Item_Type) return Boolean is <>; with function "<" (Left, Right : Item_Type) return Boolean is <>; package My_Package is function "<" (Left,Right : Some_Type) return Boolean is begin return ( Right.Reference /= null and then ( Left.Reference = null or else Left.Reference.all < Right.Reference.all ) ); end "<"; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de