comp.lang.ada
 help / color / mirror / Atom feed
From: "Yannick Duchêne (Hibou57)" <yannick_duchene@yahoo.fr>
Subject: Re: Discriminants of tagged types
Date: Thu, 28 Oct 2010 00:07:05 +0200
Date: 2010-10-28T00:07:05+02:00	[thread overview]
Message-ID: <op.vk8813e6ule2fv@garhos> (raw)
In-Reply-To: bfd2ccdb-a27b-47f0-91f0-e437ed535d4d@x42g2000yqx.googlegroups.com

I see your good point.

I have something similar for Windows API interfacing (ex. for event  
handlers whose parameters may be made of two short integer, a reference, a  
long integer, and so, in the same parameter, depending on a message ID).  
This is a particular case, because I use Unchecked_Union, and  
Unchecked_Union requires a default value for the discriminant.

Otherwise, in your example, I would say what you named Value, is not a  
Value, and rather an Expression, unless you meant an abstract view of what  
a Value is.

As there are Integer_Value, Float_Value, String_Value, I suppose each of  
these three is associated to specific operations. So why not three types ?  
Tagged types derived from a root abstract tagged type ?

And for memory management, a pool of tagged type : a pool of  
Integer_Value, a pool of Float_Value, and so on. You will even save memory  
this way. About saving memory, image you have thousands of  
ASCII_Character_Value, each of them allocating all the space which would  
be required to allocate a Float_Value, that is something between 32 and 64  
bits.

But that is easily said, as I do not know about the whole system where  
this excerpt could come from.

Still not convinced.

Le Wed, 27 Oct 2010 22:26:57 +0200, Adam Beneschan <adam@irvine.com> a  
écrit:
> I think that *modifiable* discriminants (which means default
> discriminants in Ada syntax) would be hard to do without, in variant
> record cases.  Suppose, for instance, you were using Ada to write an
> interpreter for Perl or some other language that uses variables whose
> type could change between an integer, a floating-point value, or a
> string.  It seems natural to define a variable's value as a variant
> record:
>
>    type Value (Kind : Value_Kinds := None) is record
>       case Kind is
>          when Integer_Value =>    Int : Long_Integer;  -- or your
> choice of integer type
>          when Float_Value   =>    Flt : Float;         -- ditto
>          when String_Value  =>    Str :
> Ada.Strings.Unbounded.Unbounded_String;
>       end case;
>    end record;
>
> Then, if the program assigns a new value to the variable and the value
> has a different Kind, you'd want to be able to rewrite the value
> that's stored in the symbol table entry for the variable.  In Ada you
> can do this by reassigning the entire record.  Doing this without
> modifiable discriminants would be, I believe, impossible without
> adding some access types and a level of indirection, adding the
> headaches involved with allocation, deallocation, memory leakage,
> dangling references, etc.
>
> Variant records are the main reason I use default discriminants in Ada
> code.  Another typical use is to allow arrays whose size can change
> dynamically:
>
>     MAX_LENGTH : constant := 1000;
>     subtype String_Length is 0 .. MAX_LENGTH;
>     type Variable_String (Len : String_Length) is record
>         Value : String (1 .. Len);
>     end record;
>
>                                -- Adam


-- 
Si les chats miaulent et font autant de vocalises bizarres, c’est pas pour  
les chiens.



  reply	other threads:[~2010-10-27 22:07 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-27 12:16 Discriminants of tagged types Maciej Sobczak
2010-10-27 12:34 ` Ludovic Brenta
2010-10-27 13:19   ` Dmitry A. Kazakov
2010-10-27 13:52     ` Robert A Duff
2010-10-27 14:12       ` Dmitry A. Kazakov
2010-10-27 13:44 ` Robert A Duff
2010-10-27 15:06   ` Adam Beneschan
2010-10-27 15:58     ` Yannick Duchêne (Hibou57)
2010-10-27 16:35       ` Vinzent Hoefler
2010-10-27 17:58       ` J-P. Rosen
2010-10-27 20:18         ` Yannick Duchêne (Hibou57)
2010-10-27 20:26       ` Adam Beneschan
2010-10-27 22:07         ` Yannick Duchêne (Hibou57) [this message]
2010-10-27 22:42           ` Adam Beneschan
2010-10-27 15:13   ` Maciej Sobczak
2010-10-27 16:02     ` Yannick Duchêne (Hibou57)
2010-10-27 21:13       ` Maciej Sobczak
2010-10-27 21:23         ` Ludovic Brenta
2010-10-28  8:38           ` Maciej Sobczak
2010-10-27 21:25         ` Vinzent Hoefler
2010-10-28  7:53         ` Dmitry A. Kazakov
2010-10-28  8:50           ` Maciej Sobczak
2010-10-28 10:28             ` Dmitry A. Kazakov
2010-10-28 17:48             ` Yannick Duchêne (Hibou57)
2010-10-29 18:59             ` Vinzent Hoefler
2010-10-30 21:05               ` Maciej Sobczak
2010-10-30 21:21                 ` Vinzent Hoefler
2010-10-30 22:01                   ` Yannick Duchêne (Hibou57)
2010-10-31 16:20                   ` Maciej Sobczak
2010-10-31 17:04                     ` Yannick Duchêne (Hibou57)
2010-10-31 18:36                     ` Shark8
2010-10-31 21:06                       ` Maciej Sobczak
2010-11-01  0:44                         ` Shark8
2010-11-01  9:41                           ` Maciej Sobczak
2010-10-27 16:06     ` Robert A Duff
2010-10-27 16:34       ` Yannick Duchêne (Hibou57)
2010-10-27 21:05       ` Maciej Sobczak
2010-10-28  0:35         ` Robert A Duff
2010-10-28  8:55           ` Maciej Sobczak
2010-10-27 21:28       ` Simon Wright
2010-10-27 13:54 ` J-P. Rosen
replies disabled

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