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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Experimenting with the OOP features in Ada Date: Mon, 2 Jan 2017 21:55:46 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <29380aa7-0c3b-4908-94c6-aa91f3996b42@googlegroups.com> <1c754dc2-01fc-455a-bd9a-01ecb71591af@googlegroups.com> NNTP-Posting-Host: s3c6wwRqkurrfTZpuYYZ+w.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:33009 Date: 2017-01-02T21:55:46+01:00 List-Id: On 2017-01-02 19:50, Laurent wrote: > On Monday, 2 January 2017 17:41:19 UTC+1, Dmitry A. Kazakov wrote: >> >> You create a new object each time you make assignment. So at the line >> 42 >> Create_Name creates an object with Name set. Then at the line 45 >> Create_Code_SIL overwrites that object with another instance with no >> name set. When you try to get its name, the handle of the string is >> invalid. An attempt to get the value of gives Constraint_Error. >> >> P.S. If you want to set components individually you must either expose >> >> them or else provide setters. >> >> -- >> Regards, >> Dmitry A. Kazakov >> http://www.dmitry-kazakov.de >> > Then why didn't it fail immediately on the first run? Antibiotics is a > record with 4 of those lazy Safe String objects. > > Name/=Code_SIL/=CMI/=SIR > > Each one has its own Create/Value function. Your Create function creates a whole object. > Wasn't sure/ had expected (big mistake as always) that I could somehow > use the Create/Value functions from Safe String without having to write > additional code. Of course you can. But Object you create is not a Safe_String object. > The test was actually to figure out how it would work and if it would > make a difference compared with the Unbounded_Strings package and with > the old version. If you used Unbounded_String, Name would become empty after you overwrote the object. The same behavior with Safe_String is easy to achieve. In Base_Types.Antibiotics replace Name_Value with this function Name_Value (Item : Object) return String is begin if Item.Name.Is_Valid then return Item.Name.Value; else return ""; end if; end Name_Value; (Here Ada 2005 prefix notation is used, which is much clearer) Now, there is no Constraint_Error and the result is empty string. > I am quite tempted in using the persistent storage capabilities of > Simple Components but that means that I have to adapt my program to the > style of this library. Don't think that it would be possible to only graft it > at the latest moment? It is not style, but a plain conceptual error. You are using a constructing function as if it were a setter. That won't work anywhere in any language. Replace Create_Name (Name : String) return Object; Create_Code_SIL (Name : String) return Object; ... with Set_Name (Data : in out Object; Name : String); Set_Code_SIL (Data : in out Object; Name : String); ... > BTW what is the reach of the overriding keyword i.e.: The keyword hints the compiler that the subprogram overrides the parent's implementation. If you make a mistake in the parameter profile, the compiler will reject the program. It adds a lot of safety. If not keeping it Ada 95 compatible, always use "overriding" and "not overriding" when declaring operations on tagged types. It is a good style. > Gnat was only complaining that Create_Name needs overriding even if > Create_Code_SIL is also in the Base_Type which is abstract. This a different story. Here the compiler complains that you forgot to override an operation that must be overridden. Overriding is required for abstract operations and operations returning new objects. In the first case there is no implementation to inherit from. In the second case the implementation would be most likely garbage. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de