comp.lang.ada
 help / color / mirror / Atom feed
From: "Jean-Pierre Rosen" <rosen@adalog.fr>
Subject: Re: Problem trying to implement generics.
Date: Tue, 17 Apr 2001 10:50:33 +0200
Date: 2001-04-17T10:50:33+02:00	[thread overview]
Message-ID: <9bh0uk$3rc$1@s1.read.news.oleane.net> (raw)
In-Reply-To: Pine.BSF.4.21.0104131658480.17102-100000@shell5.ba.best.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3344 bytes --]


"Brian Rogoff" <bpr@shell5.ba.best.com> a �crit dans le message news:
Pine.BSF.4.21.0104131658480.17102-100000@shell5.ba.best.com...
> > Another workaround is to declare a limited type, with a component that
> > always points to itself:
> >
> >     type T is tagged limited
> >         record
> >             Self_Reference: T_Ptr := T'Unchecked_Access;
> >             ...
> >         end record;
> >
> > If you pass this thing as an 'in' parameter to a function, the function
> > can modify it by following the Self_Reference pointer.  This works
> > because no constant objects of type T can exist.  It seems a lot like
> > "cheating", doesn't it?
>
Actually, my version is slightly more sophisticated than that . It uses an
access discriminant, avoiding the need for Unchecked_Access. See
http://www.adapower.com/lang/modifyin.html
If you consider that functions should not modify their parameters, then any
trick that allows you to modify parameters is "cheating". Now, there are two
nice things with this trick:
1) various rules of the language make sure that it can be used only where it
is safe (no dangling pointers, no pointers to the wrong thing, etc.).
2) It requires a special structure in the type itself. Therefore, it's not
the function that decides to modify the parameter, it is the parameter that
tells the function that it accepts to be modified. It's like the designer
("owner") of the type putting some special notice: "by exception to the
rule, I allow functions to modify this data". I think it removes most of the
"evil" of functions modifying parameters.

> Did anyone see this during the Ada 95 design? I thought J.P. Rosen (who
> showed most of us this evil trick :) said something about having noticed
> it early but never actually implementing it until later. I'm sure I'm
> botching the story: J.P, what's the history here?
>
I realized this possibility initially during a review meeting of tests for
access discriminants (I was a member of the ACVC/9X reviewers team). Did I
mention it at that time ? I don't remember, but if I did, it apparently went
unnoticed. Then we had that discussion about functions-modifying-parameters
on c.l.a, and I took the opportunity to address a wider audience...

BTW: one of the fascinating things with Ada is that you discover every day
paradigms, design patterns, whatever, that show that the language is even
*more* powerful than you thought. You think "wow, this is allowed by the
language. Could it possibly work?". You try it, and it's OK.

Well, I can't resist. One of my favorite ones (back in Ada83 times) is when
you have a common treatment for all exceptions, then you want to dispatch
according to the exception actually raised. For example, you have a local
file open, and you want to close it in case of an exception, then do
something.

procedure proc is
   F : File_Type;
begin
   ...
   Open (F, ...);
   ...
exception
   when others =>
      if Is_Open (F) then
         Close (F);
      end if;
      begin
         raise;   -- Reraise current exception
      exception
         when Constraint_Error =>
              ...
         when Data_Error =>
             ...
          ...
      end;
end Proc;

--
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr





  parent reply	other threads:[~2001-04-17  8:50 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-11 15:04 Problem trying to implement generics Ayende Rahien
2001-04-12  1:41 ` tmoran
2001-04-12 13:15   ` Ayende Rahien
2001-04-12 18:15     ` tmoran
2001-04-13 11:18       ` Ayende Rahien
2001-04-13 10:35         ` chris.danx
2001-04-13 11:54           ` Ayende Rahien
2001-04-13 11:49             ` chris.danx
2001-04-13 23:03               ` Ayende Rahien
2001-04-13 23:01                 ` Robert A Duff
2001-04-14  0:05                   ` Brian Rogoff
2001-04-14  1:12                     ` Ayende Rahien
2001-04-14  1:44                       ` Brian Rogoff
2001-04-14 14:03                         ` Dmitry A. Kazakov
2001-04-14 16:30                           ` Ayende Rahien
2001-04-14 16:28                             ` Michael Erdmann
2001-04-15  3:27                             ` James Rogers
2001-04-15 12:20                               ` Ayende Rahien
2001-04-15 14:09                               ` Dmitry A. Kazakov
2001-04-15 18:22                                 ` tmoran
2001-04-15 13:48                             ` Dmitry A. Kazakov
2001-04-15 20:44                               ` Ayende Rahien
2001-04-16 14:34                                 ` Dmitry A. Kazakov
2001-04-14  1:33                     ` Robert A Duff
2001-04-17  8:50                     ` Jean-Pierre Rosen [this message]
2001-04-17 13:20                   ` Tucker Taft
2001-04-17 16:51                     ` Ayende Rahien
2001-04-17 17:16                       ` Larry Hazel
2001-04-17 18:11                         ` Brian Rogoff
2001-04-17 19:10                           ` Marin David Condic
2001-04-17 21:08                             ` Brian Rogoff
2001-04-18 15:16                               ` Chad R. Meiners
2001-04-18 16:33                                 ` Marin David Condic
2001-04-17 21:09                             ` chris.danx
2001-04-17 21:11                             ` chris.danx
2001-04-17 21:17                             ` chris.danx
2001-05-08  5:40                             ` Lao Xiao Hai
2001-05-11  9:43                               ` John English
2001-05-12 19:16                                 ` Lao Xiao Hai
2001-04-17 19:32                           ` Larry Hazel
2001-04-17 21:03                           ` Ayende Rahien
2001-04-18 15:48                             ` Brian Rogoff
2001-04-20 12:34                               ` Georg Bauhaus
2001-04-20 12:42                                 ` Lutz Donnerhacke
2001-04-20 12:45                                 ` Lutz Donnerhacke
2001-04-20 19:48                                 ` Brian Rogoff
2001-04-20 20:36                                   ` David Starner
2001-04-20 23:02                                   ` Robert A Duff
2001-04-23  2:45                                     ` Brian Rogoff
2001-04-24  1:15                                       ` Robert A Duff
2001-04-24  2:00                                         ` Brian Rogoff
2001-04-24 15:12                                           ` Georg Bauhaus
2001-04-24 15:09                                         ` Georg Bauhaus
2001-04-24 18:36                                           ` Marius Amado Alves
2001-04-19 13:08                           ` Larry Kilgallen
     [not found]                           ` <9bi4g4$97m$1@nh.pace.Organization: LJK Software <YlSyXUaQmD+$@eisner.encompasserve.org>
2001-04-19 14:20                             ` Marin David Condic
2001-04-18  5:34                       ` Mike Silva
2001-04-18 16:55                       ` Ray Blaak
2001-04-24 16:00                       ` Tucker Taft
2001-04-12 13:57 ` Andy
2001-04-13  6:34   ` Simon Wright
2001-04-13 11:11   ` Ayende Rahien
2001-04-12 18:06 ` Stephen Leake
replies disabled

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