comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Variant record limitation - what's a better solution?
Date: Tue, 09 Jul 2013 08:49:33 +0100
Date: 2013-07-09T08:49:33+01:00	[thread overview]
Message-ID: <lymwpwxjea.fsf@pushface.org> (raw)
In-Reply-To: krgb4h$qe8$3@news.albasani.net

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;


  reply	other threads:[~2013-07-09  7:49 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 [this message]
2013-07-09  8:22   ` Georg Bauhaus
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