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 17:41:16 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <29380aa7-0c3b-4908-94c6-aa91f3996b42@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:33005 Date: 2017-01-02T17:41:16+01:00 List-Id: On 2017-01-02 14:21, Laurent wrote: > Trying some OOP features in Ada. So not sure if everything is actually > correct. > Comments are welcome. > > So I have done a little test. Link at the end. > I get the following error: > > raised CONSTRAINT_ERROR : my_strings-handle.adb:14 access check failed > > Which I don't understand. Sounds like the object no longer exists? > > test.adb: > > with Ada.Text_IO; > with Ada.Exceptions; > > with Gnat.Traceback.Symbolic; use Gnat; > > with Base_Types.Antibiotics; > with My_Strings; > with My_Strings.Handle; > > procedure Test is > > package SS renames My_Strings.Handle; > > Test_Antibiotic : Base_Types.Antibiotics.Object > :=Base_Types.Antibiotics.Create (Name => "Test Antibiotic 1", > Code_SIL =>"123", > CMI => "1", > SIR => "S"); > > Test_String : My_Strings.Handle.My_Safe_String := SS.Create ("Test > single 1"); > > begin > > Ada.Text_IO.Put_Line (Test_String.Value); > Test_String := SS.Create ("Test single 2"); > Ada.Text_IO.Put_Line (Test_String.Value); > Test_String := SS.Create ("Test single 3"); > Ada.Text_IO.Put_Line (Test_String.Value); > Ada.Text_IO.Put_Line (Test_String.Value); > Ada.Text_IO.New_Line; > > Ada.Text_IO.Put_Line ("Test record run 1:"); > Ada.Text_IO.New_Line; > > Ada.Text_IO.Put_Line ("Name: " & Test_Antibiotic.Name_Value); > Ada.Text_IO.Put_Line ("Code SIL: " & Test_Antibiotic.Code_SIL_Value); > Ada.Text_IO.Put_Line ("CMI: " & Test_Antibiotic.CMI_Value); > Ada.Text_IO.Put_Line ("SIR: " & Test_Antibiotic.SIR_Value); > Ada.Text_IO.New_Line; > > Ada.Text_IO.Put_Line ("Test record run 2:"); > Ada.Text_IO.New_Line; > > Test_Antibiotic := Base_Types.Antibiotics.Create_Name (Name => "Test > Antibiotic 2"); > Ada.Text_IO.Put_Line ("Name: " & Test_Antibiotic.Name_Value); > > Test_Antibiotic := Base_Types.Antibiotics.Create_Code_SIL (Code_SIL => > "amc"); > Ada.Text_IO.Put_Line ("Code SIL: " & Test_Antibiotic.Code_SIL_Value); > > Test_Antibiotic := Base_Types.Antibiotics.Create_CMI (CMI => "2"); > Ada.Text_IO.Put_Line ("CMI: " & Test_Antibiotic.CMI_Value); > > Test_Antibiotic := Base_Types.Antibiotics.Create_SIR (SIR => "R"); > Ada.Text_IO.Put_Line ("SIR: " & Test_Antibiotic.SIR_Value); > Ada.Text_IO.New_Line; > > Ada.Text_IO.Put_Line ("Test record run 3:"); > Ada.Text_IO.New_Line; > > Test_Antibiotic := Base_Types.Antibiotics.Create_Name (Name => "Test > Antibiotic 3"); > Test_Antibiotic := Base_Types.Antibiotics.Create_Code_SIL (Code_SIL => > "gen"); > Test_Antibiotic := Base_Types.Antibiotics.Create_CMI (CMI => "8"); > Test_Antibiotic := Base_Types.Antibiotics.Create_SIR (SIR => "I"); > > Ada.Text_IO.Put_Line ("Print run 3:"); > > Ada.Text_IO.Put_Line ("Name: " & Test_Antibiotic.Name_Value); > Ada.Text_IO.Put_Line ("Code SIL: " & Test_Antibiotic.Code_SIL_Value); > Ada.Text_IO.Put_Line ("CMI: " & Test_Antibiotic.CMI_Value); > Ada.Text_IO.Put_Line ("SIR: " & Test_Antibiotic.SIR_Value); > > exception > > when Err : others => > Ada.Text_IO.Put_Line ("Problem: " & > Ada.Exceptions.Exception_Information (Err)); > > Ada.Text_IO.Put_Line (Traceback.Symbolic.Symbolic_Traceback (Err)); > > end Test; > > Terminal output: > > Test single 1 > Test single 2 > Test single 3 > Test single 3 > > Test record run 1: > > Name: Test Antibiotic 1 > Code SIL: 123 > CMI: 1 > SIR: S > > Test record run 2: > > Name: Test Antibiotic 2 > Code SIL: amc > CMI: 2 > SIR: R > > Test record run 3: > > Print run 3: > Problem: raised CONSTRAINT_ERROR : my_strings-handle.adb:14 access check > failed > > > Why does it fail on the 3rd run? It works the 2 first ones? 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