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: border1.nntp.dca.giganews.com!nntp.giganews.com!goblin3!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Tue, 09 Jul 2013 10:22:33 +0200 From: Georg Bauhaus User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Variant record limitation - what's a better solution? References: <0606a658-9816-4611-84dd-4f999bf6018e@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <51dbc847$0$6577$9b4e6d93@newsspool3.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 09 Jul 2013 10:22:31 CEST NNTP-Posting-Host: 299dc918.newsspool3.arcor-online.net X-Trace: DXC=on_4]4; 4Gbk^Y=RbYBPl4`McF=Q^Z^V3h4Fo<]lROoRa8kF_ljHilPCY\c7>ejVhEJ0JIOO5>FbbSBR75MG_3d X-Complaints-To: usenet-abuse@arcor.de Date: 2013-07-09T10:22:31+02:00 List-Id: On 09.07.13 08:38, Peter Brooks wrote: > function load(x : lorry_size := small) return small_lorry_load; > function load(x : lorry_size := medium) return medium_lorry_load; > function load(x : lorry_size := large) return large_lorry_load; > > > type > lorry_type(lorry : lorry_size) is > record > number_plate : lorry_number_plate; > location : depot_name_type; > destination : depot_name_type; > case lorry is > when small => lorry_load : load(lorry); > when medium => lorry_load : load(lorry); > when large => lorry_load : load(lorry); > end case; > end record; > " > > I know that the syntax is wrong - I've been trying all sorts of alternatives. What have I not understood? Adam's and Simon's solutions should work better, I think. This one will make a discriminant check fail if a caller mistakenly loads a large size from a small lorry, say. type lorry_type(lorry : lorry_size) is private; function load(L : Lorry_type) return small_lorry_load; function load(L : Lorry_type) return medium_lorry_load; function load(L : Lorry_type) return large_lorry_load; private type lorry_type(lorry : lorry_size) is record number_plate : lorry_number_plate; location : depot_name_type; destination : depot_name_type; case lorry is when small => small_load : small_lorry_load; when medium => medium_load : medium_lorry_load; when large => large_load : large_lorry_Load; end case; end record; The bodies of the load functions will be function load(L : Lorry_type) return Medium_Lorry_Load is begin return L.Medium_Load; end Load; and similarly for the other two functions. (They are operation of type lorry_type, hence the parameter L). A caller will then declare x : large_lorry_load; begin x := load(my_lorry); end after which X will have a copy of the large component of L, provided the discriminant matches, i.e. my_lorry.lorry = large. If not, the program raises constraint_error.