From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: Variant record limitation - what's a better solution?
Date: Tue, 09 Jul 2013 10:22:33 +0200
Date: 2013-07-09T10:22:31+02:00 [thread overview]
Message-ID: <51dbc847$0$6577$9b4e6d93@newsspool3.arcor-online.net> (raw)
In-Reply-To: <krgb4h$qe8$3@news.albasani.net>
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.
next prev parent reply other threads:[~2013-07-09 8:22 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-03 7:52 Variant record limitation - what's a better solution? Peter Brooks
2013-07-03 8:11 ` Georg Bauhaus
2013-07-03 9:39 ` Peter Brooks
2013-07-03 16:23 ` Jeffrey Carter
2013-07-03 18:35 ` Shark8
2013-07-03 19:26 ` Adam Beneschan
2013-07-09 11:48 ` Peter Brooks
2013-07-09 15:11 ` Adam Beneschan
2013-07-10 1:11 ` Peter Brooks
2013-07-03 20:55 ` Per Sandberg
2013-07-09 6:38 ` Peter Brooks
2013-07-09 7:49 ` Simon Wright
2013-07-09 8:22 ` Georg Bauhaus [this message]
2013-07-09 14:12 ` Eryndlia Mavourneen
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox