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.dca3.giganews.com!border2.nntp.dca3.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!goblin3!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Variant record limitation - what's a better solution? Date: Tue, 09 Jul 2013 08:49:33 +0100 Organization: A noiseless patient Spider Message-ID: References: <0606a658-9816-4611-84dd-4f999bf6018e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx05.eternal-september.org; posting-host="a849a84efccf4ec3e3fdf530b5c53bc9"; logging-data="7775"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188sgc9X8ztfwl6WRps+M+fih6sujYgfWY=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (darwin) Cancel-Lock: sha1:d06cBNj4qhj0NFPZbt/6h4Q3nik= sha1:wvcQfW5bmNpZHucy79ah5ClD+jA= X-Original-Bytes: 2923 Xref: number.nntp.dca.giganews.com comp.lang.ada:182360 Date: 2013-07-09T08:49:33+01:00 List-Id: peter.h.m.brooks@gmail.com (Peter Brooks) writes: > I'm still having trouble with this. I've tried using functions, as > suggested, but it doesn't allow overloaded functions in the varient > part. Here's what I tried: I think that people meant you should hide the awkwardness of the *unavoidably* different names by using functions to encapsulate the differences. Something like this (using Ada2012 expression functions, hope that doesn't confuse): package Lorries is type Lorry_Load is new Integer range 1 .. 36; type Lorry_Size is (Small, Medium, Large); type Lorry_Type (Lorry : Lorry_Size) is private; type Lorry_Number_Plate is new String (1 .. 8); function Plate (L : Lorry_Type) return Lorry_Number_Plate; function Load (L : Lorry_Type) return Lorry_Load; private subtype Small_Lorry_Load is Lorry_Load range 1 .. 2; subtype Medium_Lorry_Load is Lorry_Load range 1 .. 18; subtype Large_Lorry_Load is Lorry_Load range 1 .. 36; 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; function Plate (L : Lorry_Type) return Lorry_Number_Plate is (L.Number_Plate); function Load (L : Lorry_Type) return Lorry_Load is (case L.Lorry is when Small => L.Small_Load, when Medium => L.Medium_Load, when Large => L.Large_Load); end Lorries;