comp.lang.ada
 help / color / mirror / Atom feed
From: mark_doherty@yahoo.co.uk (Mark Doherty)
Subject: Re: Different initial values for derived type?
Date: 10 Nov 2003 04:56:25 -0800
Date: 2003-11-10T04:56:25-08:00	[thread overview]
Message-ID: <5e3e03a7.0311100456.1b41c70a@posting.google.com> (raw)
In-Reply-To: urhlob.092.ln@ID-soos.user.dfncis.de

Stefan Soos <stefan.soos@gmx.de> wrote in message news:<urhlob.092.ln@ID-soos.user.dfncis.de>...
> Hello,
> 
> I've got the following type definition
> 
>      type Object_Type is tagged 
> 	  record
> 		Name  : Character := '?';
> 		Score : Natural   := 0;
> 		X	: Positive  := 1;
> 		Y     : Positive  := 1;
> 	  end record;
> Now I'd like to have a derived type with the same components
> but with different initial Values. Do I have to use discriminants for
> all of my components or can I say something like
> 
>     type Derived_Type is new Object_Type with ( Name  => '!', 
> 					        Score => 10); 
> I know, I can't because it doesn't compile, but is there a
> similar approach?
> 
> 
> Thanks in advance,
> Stefan
> 
> --

you could use controlled types if performance is not an issue...

with ada.finalization; use ada.finalization;

package controlled_types is
   type Object_Type is new controlled with     
   record
	Name  : Character;
   end record;
   procedure initialize (object : in out object_type);
   procedure ADJUST (object : in out object_type);
   procedure finalize (object : in out object_type);

   type Derived_Type is new Object_Type with null record;
   
   procedure initialize (object : in out derived_type);
   procedure ADJUST (object : in out derived_type);
   procedure finalize (object : in out derived_type);

end ;

package body controlled_types is

   procedure initialize (object : in out object_type) is

   begin
      object := ( controlled with
      Name   => '?');
   end;

   procedure ADJUST (object : in out object_type) is
   begin
      null;
   end;

   procedure finalize (object : in out object_type) is
    begin
      null;
   end;

   procedure initialize (object : in out derived_type) is
   begin
      object := (controlled with
      Name   => '!');
   end;

   procedure ADJUST (object : in out derived_type) is
   begin
      null;
   end;

   procedure finalize (object : in out derived_type) is
   begin
      null;
   end;

end ;


with controlled_types; use controlled_types;
with ada.text_io; use ada.text_io;

procedure controlled_test is
   o : object_type;
   d : derived_type;
begin
   put_line("name => " & o.name);
   put_line("name => " & d.name);
end;



      reply	other threads:[~2003-11-10 12:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-09 14:10 Different initial values for derived type? Stefan Soos
2003-11-10 12:56 ` Mark Doherty [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox