From: Gautier <gautier@fakeaddress.nil>
Subject: Re: Limited_Controlled and constructor functions
Date: Sat, 20 Jan 2007 18:09:56 +0100
Date: 2007-01-20T18:09:56+01:00 [thread overview]
Message-ID: <45b24cbd$1_6@news.bluewin.ch> (raw)
In-Reply-To: <eoptnu$62f$1@cernne03.cern.ch>
Maciej Sobczak:
> What I want to accomplish is the functional way of building lists of
> values of variant types:
>
> P : constant Params := Make_Params(Param("Hello"),
> Param("Ada"),
> Param(12),
> Param(3.14)));
Would you be happy with the following ? For the fun, I did it in Ada 83 :-).
You can change that with Unbounded_String's and make things nicely, hidden,
private, tagged and so on as you like... Note that the function Make_Params
was superfluous in that implementation.
Output:
Parameter # 1: [Hello]
Parameter # 2: [Ada]
Parameter # 3: [ 12]
Parameter # 4: [ 3.14000E+00]
--8<-----------8<-----------8<-----------8<-----------8<-----------8<---------
-- start 17:35
-- stop 17:59
pragma Ada_83;
with Text_IO, Variant_pkg;
procedure Variants is
use Variant_pkg;
P : constant Params := (Param("Hello"),
Param("Ada"),
Param(12),
Param(3.14));
begin
for i in P'Range loop
Text_IO.Put_Line(
"Parameter #" & Integer'Image(i) & ": [" & Image(P(i).all) & ']'
);
end loop;
end Variants;
--8<-----------8<-----------8<-----------8<-----------8<-----------8<---------
package Variant_pkg is
type BorString( maxlength: Positive ) is record
length: Natural:= 0;
s: String( 1..maxlength );
end record;
type Kind is (A_String, An_Integer, A_Float);
xamax: constant:= 1024;
type Atom(k: Kind) is record
case k is
when A_String => s: BorString(xamax);
when An_Integer => i: Integer;
when A_Float => f: Float;
end case;
end record;
type p_Atom is access Atom;
type Params is array(Positive range <>) of p_Atom;
function Param(s: String) return p_Atom;
function Param(i: Integer) return p_Atom;
function Param(f: Float) return p_Atom;
function Image(a: Atom) return String;
end Variant_pkg;
--8<-----------8<-----------8<-----------8<-----------8<-----------8<---------
package body Variant_pkg is
function Param(s: String) return p_Atom is
a: p_Atom:= new Atom(A_String);
begin
a.s.s(1..s'Length):= s;
a.s.length:= s'Length;
return a;
end Param;
function Param(i: Integer) return p_Atom is
a: p_Atom:= new Atom(An_Integer);
begin
a.i:= i;
return a;
end Param;
function Param(f: Float) return p_Atom is
a: p_Atom:= new Atom(A_Float);
begin
a.f:= f;
return a;
end Param;
function Image(a: Atom) return String is
begin
case a.k is
when A_String => return a.s.s(1..a.s.length);
when An_Integer => return Integer'Image(a.i);
when A_Float => return Float'Image(a.f);
end case;
end Image;
end Variant_pkg;
______________________________________________________________
Gautier -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm
NB: For a direct answer, e-mail address on the Web site!
next prev parent reply other threads:[~2007-01-20 17:09 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-01-18 9:51 Limited_Controlled and constructor functions Maciej Sobczak
2007-01-18 12:13 ` AW: " Grein, Christoph (Fa. ESG)
2007-01-18 16:41 ` Robert A Duff
2007-01-19 7:58 ` Maciej Sobczak
2007-01-19 9:41 ` Dmitry A. Kazakov
2007-01-19 13:45 ` Maciej Sobczak
2007-01-19 14:33 ` Dmitry A. Kazakov
2007-01-22 8:59 ` Maciej Sobczak
2007-01-20 17:09 ` Gautier [this message]
2007-01-20 19:39 ` Gautier
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox