From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ce0900b60ca3f616 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-11-09 20:39:35 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!ppp-1-20.cvx1.telinco.NET!not-for-mail From: "Nick Roberts" Newsgroups: comp.lang.ada Subject: Re: Better control of assignment Date: Sat, 10 Nov 2001 00:32:42 -0000 Message-ID: <9sib24$13aeg3$2@ID-25716.news.dfncis.de> References: <9rti6v$hcu$1@news.huji.ac.il> <9sdnb2$dd4$1@news.huji.ac.il> <9seup3$12h0ar$2@ID-25716.news.dfncis.de> <3BEC12BA.E72A81EC@boeing.com> NNTP-Posting-Host: ppp-1-20.cvx1.telinco.net (212.1.136.20) X-Trace: fu-berlin.de 1005367173 37042691 212.1.136.20 (16 [25716]) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Xref: archiver1.google.com comp.lang.ada:16197 Date: 2001-11-10T00:32:42+00:00 List-Id: "Jeffrey Carter" wrote in message news:3BEC12BA.E72A81EC@boeing.com... > Nick Roberts wrote: > > procedure T'Construct (Object: in out T); > > procedure T'Destroy (Object: in out T); > > procedure T'Assign (Source, Target: in out T); > > Assignment should not be able to modify the RHS, especially as it may be > an expression, not a variable. Perhaps the name Assign is wrong. Maybe Copy would be better. To my mind, it seems obvious (of course ;-) that if the RHS is a constant, this procedure is not invoked; it is only invoked for a copy (including parameter passing by copy (if selected)) from one variable object of type T to another. If this does not deliver enough control, you must make the type private. For example: type Odd_Counter is new Integer; procedure Assign_Odd (Src, Tgt: in out Odd_Counter); for Odd_Counter'Copy use Assign_Odd; ... procedure Assign_Odd (Src, Tgt: in out Odd_Counter) is begin if not Is_Odd(Src) then raise Oddness_Error; else Tgt := Src; -- inherited assignment used here by a language rule end if; end; The idea is that an object of type Odd_Counter can only ever hold an odd number. But nothing prevents: C: Odd_Counter := 2; -- oddness violation, but it will not be caught So we must instead declare (in a package specification): type Odd_Counter is private; ... procedure Init (Ctr: in out Odd_Counter; Int: in Integer); ... private type Odd_Counter is new Integer; ... And then (in the package body) we can check for oddness: procedure Init (Ctr: in out Odd_Counter; Int: in Integer) is begin if not Is_Odd(Int) then raise Oddness_Error; else Ctr := Odd_Counter(Int); end if; end; Of course, in this case, if we want the bastion of operations pre-defined for Integer, we must explicitly declare functions for "+", "-", "*", "/" and so on. Too bad. > I was thinking recently of how to provide a different implementation of > Ada.Strings.Unbounded, and came to the conclusion that the current > controlled-type handling of user-defined assignment is not able to > handle the concept I had in mind. Of course, I might be wrong about > this, and would be happy if I am. I believe my proposal would solve the problem of the kind of implementation of Ada unbounded strings that Jeff suggests. -- Nick Roberts