comp.lang.ada
 help / color / mirror / Atom feed
* Different initial values for derived type?
@ 2003-11-09 14:10 Stefan Soos
  2003-11-10 12:56 ` Mark Doherty
  0 siblings, 1 reply; 2+ messages in thread
From: Stefan Soos @ 2003-11-09 14:10 UTC (permalink / raw)


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

-- 




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Different initial values for derived type?
  2003-11-09 14:10 Different initial values for derived type? Stefan Soos
@ 2003-11-10 12:56 ` Mark Doherty
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Doherty @ 2003-11-10 12:56 UTC (permalink / raw)


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;



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-11-10 12:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-09 14:10 Different initial values for derived type? Stefan Soos
2003-11-10 12:56 ` Mark Doherty

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