comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Discriminants of tagged types
Date: Wed, 27 Oct 2010 15:42:49 -0700 (PDT)
Date: 2010-10-27T15:42:49-07:00	[thread overview]
Message-ID: <79d95094-50a0-4748-a347-c44c4cf5c81f@f20g2000vbc.googlegroups.com> (raw)
In-Reply-To: op.vk8813e6ule2fv@garhos

On Oct 27, 3:07 pm, Yannick Duchêne (Hibou57)
<yannick_duch...@yahoo.fr> wrote:
> 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.

Well, this was just intended to be an example, and it's best not to
get hung up on one example.  The idea was something like: suppose
you're writing an Ada interpreter for the Perl language, which does
not have strong typing.  A Perl program can have a variable named $x,
and the program could, at one point, assign $x=3; to give it an
integer value, and later $x="This is a string"; to give it a string
value, say.  The Ada interpreter would likely have some lookup table
in which it would look up the name "$x" and this would give it some
information about the current state of $x, with one of those pieces of
information being the current value of $x.  The idea here is that for
a Perl assignment statement, the interpreter could look up the record
containing $x and then reassign the value in that record to a new
value, which would involve changing the discriminant.  (Of course,
this is a huge oversimplification of the Perl language.  But it's the
sort of thing that could work in simpler interpreters---and, in fact,
I have used Ada for tools with command-line interfaces that would be
considered simple interpreters.  Also, of course, there are many other
possible uses for variant records that have nothing to do with
interpreters.)


> 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 ?

Because the Ada record that contains information about $x cannot have
a component of type Root_Value_Type'Class.  The component would have
to be of an *access* type to Root_Value_Type'Class, and that's where
you start having to worry about allocation and memory leakage and the
like.  It's certainly one way to solve the problem, but it's not
without cost, and for a *simple* case (Perl would not be a simple
case, so it's a bad example), a variant record could be better.
Memory management is never as simple as it seems.

                                -- Adam



  reply	other threads:[~2010-10-27 22:42 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)
2010-10-27 22:42           ` Adam Beneschan [this message]
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