* strange length check error @ 2018-01-16 20:48 Mehdi Saada 2018-01-17 1:42 ` Anh Vo ` (2 more replies) 0 siblings, 3 replies; 20+ messages in thread From: Mehdi Saada @ 2018-01-16 20:48 UTC (permalink / raw) This : Put(Ch_Bounded1 & ' ' ); Calls on that: function "&" (Left : in Bounded_String; Right : in Character) return Bounded_String is begin if LENGTH(LEFT) + 1 > MAX_LENGTH then raise LENGTH_ERROR with "ERROR '&': LEFT.LENGTH := " & NATURAL'IMAGE(LENGTH(LEFT)) & " + 1 > MAX_LENGTH"; else @@@ return (LENGTH(LEFT) + 1, LEFT.DATA(1..LENGTH(LEFT)) & RIGHT); end if; end "&"; The line with @@@ raises this: raised CONSTRAINT_ERROR : p_bounded_g-p_new_bounded_g.adb:34 length check failed The value of CH_BOUNDED1 at that moment is "pour tester le debut", its length is 21, and MAX_LENGTH est 80. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-16 20:48 strange length check error Mehdi Saada @ 2018-01-17 1:42 ` Anh Vo 2018-01-17 9:23 ` Simon Wright 2018-01-17 8:49 ` Dmitry A. Kazakov 2018-01-23 16:18 ` Robert Eachus 2 siblings, 1 reply; 20+ messages in thread From: Anh Vo @ 2018-01-17 1:42 UTC (permalink / raw) On Tuesday, January 16, 2018 at 12:48:45 PM UTC-8, Mehdi Saada wrote: > This : Put(Ch_Bounded1 & ' ' ); > Calls on that: > function "&" > (Left : in Bounded_String; > Right : in Character) > return Bounded_String > is > begin > if LENGTH(LEFT) + 1 > MAX_LENGTH then > raise LENGTH_ERROR with "ERROR '&': LEFT.LENGTH := " & NATURAL'IMAGE(LENGTH(LEFT)) & " + 1 > MAX_LENGTH"; > else > @@@ return (LENGTH(LEFT) + 1, LEFT.DATA(1..LENGTH(LEFT)) & RIGHT); > end if; > end "&"; > > The line with @@@ raises this: > raised CONSTRAINT_ERROR : p_bounded_g-p_new_bounded_g.adb:34 length check failed > The value of CH_BOUNDED1 at that moment is "pour tester le debut", its length is 21, and MAX_LENGTH est 80. What is Bounded_String? ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 1:42 ` Anh Vo @ 2018-01-17 9:23 ` Simon Wright 2018-01-17 12:13 ` Mehdi Saada 2018-01-17 13:15 ` Simon Clubley 0 siblings, 2 replies; 20+ messages in thread From: Simon Wright @ 2018-01-17 9:23 UTC (permalink / raw) Anh Vo <anhvofrcaus@gmail.com> writes: > On Tuesday, January 16, 2018 at 12:48:45 PM UTC-8, Mehdi Saada wrote: >> This : Put(Ch_Bounded1 & ' ' ); >> Calls on that: >> function "&" >> (Left : in Bounded_String; >> Right : in Character) >> return Bounded_String >> is >> begin >> if LENGTH(LEFT) + 1 > MAX_LENGTH then >> raise LENGTH_ERROR with "ERROR '&': LEFT.LENGTH := " & NATURAL'IMAGE(LENGTH(LEFT)) & " + 1 > MAX_LENGTH"; >> else >> @@@ return (LENGTH(LEFT) + 1, LEFT.DATA(1..LENGTH(LEFT)) & RIGHT); >> end if; >> end "&"; >> >> The line with @@@ raises this: >> raised CONSTRAINT_ERROR : p_bounded_g-p_new_bounded_g.adb:34 length check failed >> The value of CH_BOUNDED1 at that moment is "pour tester le debut", its length is 21, and MAX_LENGTH est 80. > > What is Bounded_String? I agree. It's not the one from the standard library ((a) it's different, (b) the standard library doesn't use this ugly, hard to read, identifiers-in-upper-case style). This shows why claims that you never need to use the debugger when you write in Ada are wrong. "What, never?" "Well,hardly ever." ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 9:23 ` Simon Wright @ 2018-01-17 12:13 ` Mehdi Saada 2018-01-17 13:19 ` AdaMagica 2018-01-17 13:15 ` Simon Clubley 1 sibling, 1 reply; 20+ messages in thread From: Mehdi Saada @ 2018-01-17 12:13 UTC (permalink / raw) I get it, it's ugly, inefficient, wrong, I KNOW, but it's not mine, and I can't affor rewriting *every* piece of code that come along the exercices. Well, I could replace the actual type BOUNDED_STRING is record LENGTH: NATURAL := 0; DATA: STRING; end record; by: type Bounded_String (Length: Natural := 0) is record Data: String (1..Length); end record; I suppose it wouldn't impact the specification a lot. But I guess it wouldn't be enough to please you, Dmitry ? I swear I'll try your strings later, when the *teacher*'s exemples will work as they should. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 12:13 ` Mehdi Saada @ 2018-01-17 13:19 ` AdaMagica 2018-01-17 13:41 ` Simon Wright 2018-01-17 13:43 ` Mehdi Saada 0 siblings, 2 replies; 20+ messages in thread From: AdaMagica @ 2018-01-17 13:19 UTC (permalink / raw) Am Mittwoch, 17. Januar 2018 13:13:49 UTC+1 schrieb Mehdi Saada: > I get it, it's ugly, inefficient, wrong, I KNOW, but it's not mine, and I can't affor rewriting *every* piece of code that come along the exercices. > Well, I could replace the actual > type BOUNDED_STRING is record > LENGTH: NATURAL := 0; > DATA: STRING (1 .. 80); <---- I guess *you* forgot this or else it's illegal > end record; Then of course this will raise an exception: return (LENGTH(LEFT) + 1, LEFT.DATA(1..LENGTH(LEFT)) & RIGHT); since LEFT.DATA(1..LENGTH(LEFT)) & RIGHT is shorter than 80. Who is the *teacher*? ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 13:19 ` AdaMagica @ 2018-01-17 13:41 ` Simon Wright 2018-01-17 21:37 ` Jeffrey R. Carter 2018-01-17 13:43 ` Mehdi Saada 1 sibling, 1 reply; 20+ messages in thread From: Simon Wright @ 2018-01-17 13:41 UTC (permalink / raw) AdaMagica <christ-usch.grein@t-online.de> writes: > Am Mittwoch, 17. Januar 2018 13:13:49 UTC+1 schrieb Mehdi Saada: >> I get it, it's ugly, inefficient, wrong, I KNOW, but it's not mine, >> and I can't affor rewriting *every* piece of code that come along >> the exercices. >> Well, I could replace the actual >> type BOUNDED_STRING is record >> LENGTH: NATURAL := 0; >> DATA: STRING (1 .. 80); <---- I guess *you* forgot this or else >> it's illegal >> end record; > > Then of course this will raise an exception: > return (LENGTH(LEFT) + 1, LEFT.DATA(1..LENGTH(LEFT)) & RIGHT); > since > LEFT.DATA(1..LENGTH(LEFT)) & RIGHT is shorter than 80. > > Who is the *teacher*? I'm guessing that this is a generic, with parameter MAX_LENGTH, so maybe type BOUNDED_STRING is record LENGTH: NATURAL := 0; DATA: STRING (1 .. MAX_LENGTH); end record; & then this should be OK return (LENGTH(LEFT) + 1, LEFT.DATA(1..LENGTH(LEFT)) & RIGHT & LEFT.DATA((LENGTH(LEFT) + 1)..MAX_LENGTH)); ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 13:41 ` Simon Wright @ 2018-01-17 21:37 ` Jeffrey R. Carter 2018-01-18 11:12 ` Simon Wright 0 siblings, 1 reply; 20+ messages in thread From: Jeffrey R. Carter @ 2018-01-17 21:37 UTC (permalink / raw) On 01/17/2018 02:41 PM, Simon Wright wrote: > > return > (LENGTH(LEFT) + 1, > LEFT.DATA(1..LENGTH(LEFT)) > & RIGHT > & LEFT.DATA((LENGTH(LEFT) + 1)..MAX_LENGTH)); I think the last part should be Left.Data (Left.Length + 1 .. Max_Length - 1) or (Left.Length + 2 .. Max_Length). I guess it doesn't matter what you put in those unused positions; you could also have (Left.Length + 2 .. Max_Length => <>). I'd probably have written Result : Bounded_String; begin -- "&" Result.Length := Left.Length + 1; Result.Data (1 .. Result.Length) := Left (1 .. Left.Length) & Right; return Result; -- Jeff Carter "We'll make Rock Ridge think it's a chicken that got caught in a tractor's nuts!" Blazing Saddles 87 ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 21:37 ` Jeffrey R. Carter @ 2018-01-18 11:12 ` Simon Wright 0 siblings, 0 replies; 20+ messages in thread From: Simon Wright @ 2018-01-18 11:12 UTC (permalink / raw) "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> writes: > On 01/17/2018 02:41 PM, Simon Wright wrote: >> >> return >> (LENGTH(LEFT) + 1, >> LEFT.DATA(1..LENGTH(LEFT)) >> & RIGHT >> & LEFT.DATA((LENGTH(LEFT) + 1)..MAX_LENGTH)); > > I think the last part should be Left.Data (Left.Length + 1 > .. Max_Length - 1) or (Left.Length + 2 .. Max_Length). I guess it > doesn't matter what you put in those unused positions; you could also > have (Left.Length + 2 .. Max_Length => <>). Should have been (Left.Length + 2 .. Max_Length). That'll teach me not to post code without testing it .. > I'd probably have written > > Result : Bounded_String; > begin -- "&" > Result.Length := Left.Length + 1; > Result.Data (1 .. Result.Length) := Left (1 .. Left.Length) & Right; > > return Result; There was a check in the original that the input wasn't already at its max length. That aside, agreed. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 13:19 ` AdaMagica 2018-01-17 13:41 ` Simon Wright @ 2018-01-17 13:43 ` Mehdi Saada 2018-01-17 14:03 ` Mehdi Saada 1 sibling, 1 reply; 20+ messages in thread From: Mehdi Saada @ 2018-01-17 13:43 UTC (permalink / raw) Sorry. It was indeed Data: String (1..Max_Length) wher Max_Length is 80. Teacher is Daniel Feneuille. French. But I understand now... I used to assign strings of different length. Of course it's illegal. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 13:43 ` Mehdi Saada @ 2018-01-17 14:03 ` Mehdi Saada 2018-01-17 16:15 ` Mehdi Saada 0 siblings, 1 reply; 20+ messages in thread From: Mehdi Saada @ 2018-01-17 14:03 UTC (permalink / raw) Sorry. It was indeed Data: String (1..Max_Length) wher Max_Length is 80. Teacher is Daniel Feneuille. French. But I understand now... I used to assign strings of different length. I'm rewriting things accordingly. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 14:03 ` Mehdi Saada @ 2018-01-17 16:15 ` Mehdi Saada 2018-01-17 19:43 ` Simon Wright 0 siblings, 1 reply; 20+ messages in thread From: Mehdi Saada @ 2018-01-17 16:15 UTC (permalink / raw) It's done. I ran the test program, and fortunately, it tells nothing about the core package I spent time on. At least it complies with the specification, though the behavior might be partialy wrong, I'll see that later. But it still shows error with IO child package, which I modified too but contains only: function Get_Line ( File : File_Type := Standard_Input ) return Bounded_String; procedure Put_Line ( File : File_Type := Standard_Output; Item : in Bounded_String ); procedure Put ( File : File_Type := Standard_Output; Item : in Bounded_String ); as specs, and as bodies: procedure Put ( File : File_Type := Standard_Output; Item : in Bounded_String ) is begin Put(File, Item.Data ); end Put; procedure Put_Line ( File : File_Type := Standard_Output; Item : in Bounded_String ) is begin Put_Line (File, Item.Data ); end Put_Line; function Get_Line ( File : File_Type := Standard_Input ) return Bounded_String is Ch : Unbounded_String; begin Ch := Get_Line(File); return (Length(Ch), Slice(Ch,1,Length(Ch))); end Get_Line; Originally, there was subprograms with, and without the File parameters. I didn't get why, and put instead default file parameters. I suspect there is a reason I never saw such default parameters in generic package before, but I couldn't help but give a try... There are the errors: crea_ora.adb:32:07: no candidate interpretations match the actuals: crea_ora.adb:32:07: too many arguments in call to "Put_Line" crea_ora.adb:32:17: expected private type "Ada.Text_Io.File_Type" crea_ora.adb:32:17: found private type "Bounded_String" defined at p_bounded_g.ads:21, instance at p_bounded_80.ads:6 crea_ora.adb:32:17: ==> in call to "Put_Line" at p_bounded_g-p_bounded_g_io.ads:17, instance at p_bounded_io_80.ads:9 crea_ora.adb:32:17: ==> in call to "Put_Line" at a-textio.ads:259 One such piece for each call on Put, Put_Line or Get_Line. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 16:15 ` Mehdi Saada @ 2018-01-17 19:43 ` Simon Wright 2018-01-17 21:37 ` Mehdi Saada 0 siblings, 1 reply; 20+ messages in thread From: Simon Wright @ 2018-01-17 19:43 UTC (permalink / raw) Not sure what the problem is, but this looks like a case where you should *not* 'use Ada.Text_IO;', especially in the spec. Unrelated - do you think that in your procedure Put, for example, you should only output the valid contents of Item.Data? (1 .. Item.Length) ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 19:43 ` Simon Wright @ 2018-01-17 21:37 ` Mehdi Saada 2018-01-17 23:00 ` Niklas Holsti 0 siblings, 1 reply; 20+ messages in thread From: Mehdi Saada @ 2018-01-17 21:37 UTC (permalink / raw) Ah ? Ah how am I gonna use File_Type then ? I can't do otherwise. There is only valid content, since now the definition is type Bounded_String (Length: Length_Range) is Data: String; end record; I changed everything accordingly. The package merely creates bigger or smaller Strings dynamically. I don't use Text_IO outside the IO child package. If there is a way to do IO without Ada.Text_IO, I'm open. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 21:37 ` Mehdi Saada @ 2018-01-17 23:00 ` Niklas Holsti 2018-01-18 11:07 ` Simon Wright 0 siblings, 1 reply; 20+ messages in thread From: Niklas Holsti @ 2018-01-17 23:00 UTC (permalink / raw) On 18-01-17 21:43 , Simon Wright wrote: > Not sure what the problem is, but this looks like a case where you > should *not* 'use Ada.Text_IO;', especially in the spec. On 18-01-17 23:37 , Mehdi Saada wrote: > Ah ? Ah how am I gonna use File_Type then ? I can't do otherwise. By qualifying with the name of the package in which it is defined: function Get_Line ( File : Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Input ) return Bounded_String; > If there is a way to do IO without Ada.Text_IO, I'm open. As I understood Simon (but perhaps I should have let him answer), the point was to say only with Ada.Text_IO; but *not* add use Ada.Text_IO; because you are defining operations (Get_Line, Put_Line, ...) with the same names as operations in Ada.Text_IO, and you could get into problems with one definition hiding another. The question "to 'use', or not to 'use'" is a big discussion in Ada-land, with people having different styles and preferences. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ . ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 23:00 ` Niklas Holsti @ 2018-01-18 11:07 ` Simon Wright 0 siblings, 0 replies; 20+ messages in thread From: Simon Wright @ 2018-01-18 11:07 UTC (permalink / raw) Niklas Holsti <niklas.holsti@tidorum.invalid> writes: > As I understood Simon (but perhaps I should have let him answer), the > point was to say only > > with Ada.Text_IO; > > but *not* add > > use Ada.Text_IO; Yes. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-17 9:23 ` Simon Wright 2018-01-17 12:13 ` Mehdi Saada @ 2018-01-17 13:15 ` Simon Clubley 1 sibling, 0 replies; 20+ messages in thread From: Simon Clubley @ 2018-01-17 13:15 UTC (permalink / raw) On 2018-01-17, Simon Wright <simon@pushface.org> wrote: > > I agree. It's not the one from the standard library ((a) it's different, > (b) the standard library doesn't use this ugly, hard to read, > identifiers-in-upper-case style). > Someone was obviously following the Ada 83 style when they wrote that code... Not as annoying as some Wirth case sensitive languages however where the keywords themselves are in uppercase. (Oberon comes to mind here.) Simon. -- Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP Microsoft: Bringing you 1980s technology to a 21st century world ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-16 20:48 strange length check error Mehdi Saada 2018-01-17 1:42 ` Anh Vo @ 2018-01-17 8:49 ` Dmitry A. Kazakov 2018-01-23 16:18 ` Robert Eachus 2 siblings, 0 replies; 20+ messages in thread From: Dmitry A. Kazakov @ 2018-01-17 8:49 UTC (permalink / raw) On 16/01/2018 21:48, Mehdi Saada wrote: > This : Put(Ch_Bounded1 & ' ' ); > Calls on that: > function "&" > (Left : in Bounded_String; > Right : in Character) > return Bounded_String > is > begin > if LENGTH(LEFT) + 1 > MAX_LENGTH then > raise LENGTH_ERROR with "ERROR '&': LEFT.LENGTH := " & NATURAL'IMAGE(LENGTH(LEFT)) & " + 1 > MAX_LENGTH"; > else > @@@ return (LENGTH(LEFT) + 1, LEFT.DATA(1..LENGTH(LEFT)) & RIGHT); > end if; > end "&"; > > The line with @@@ raises this: > raised CONSTRAINT_ERROR : p_bounded_g-p_new_bounded_g.adb:34 length check failed > The value of CH_BOUNDED1 at that moment is "pour tester le debut", its length is 21, and MAX_LENGTH est 80. Why do you care? It is a wrong design anyway. The whole idea of bounded-length strings is avoid reallocation and copying upon string modification. [Which is why you should never use them] Thus, *all* operations on bounded-length strings must be in-place, except constructors. If you have "&" that defeats the only purpose of having such strings. Use String. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-16 20:48 strange length check error Mehdi Saada 2018-01-17 1:42 ` Anh Vo 2018-01-17 8:49 ` Dmitry A. Kazakov @ 2018-01-23 16:18 ` Robert Eachus 2018-01-23 16:41 ` Mehdi Saada 2 siblings, 1 reply; 20+ messages in thread From: Robert Eachus @ 2018-01-23 16:18 UTC (permalink / raw) On Tuesday, January 16, 2018 at 3:48:45 PM UTC-5, Mehdi Saada wrote: > This : Put(Ch_Bounded1 & ' ' ); > Calls on that: > function "&" > (Left : in Bounded_String; > Right : in Character) > return Bounded_String > is ... > @@@ return (LENGTH(LEFT) + 1, LEFT.DATA(1..LENGTH(LEFT)) & RIGHT); > end if; > end "&"; > > The line with @@@ raises this: > raised CONSTRAINT_ERROR : p_bounded_g-p_new_bounded_g.adb:34 length check failed > The value of CH_BOUNDED1 at that moment is "pour tester le debut", its length is 21, and MAX_LENGTH est 80. Arg! Horrible code, I'm not surprised that others missed it. This is a recursive call on "&" with a longer LENGTH each time. Eventually LENGTH equals MAX_LENGTH and the error is raised. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-23 16:18 ` Robert Eachus @ 2018-01-23 16:41 ` Mehdi Saada 2018-01-24 16:22 ` Robert Eachus 0 siblings, 1 reply; 20+ messages in thread From: Mehdi Saada @ 2018-01-23 16:41 UTC (permalink / raw) > Arg! Horrible code, I'm not surprised that others missed it. This is a recursive call on "&" with a longer LENGTH each time. Eventually LENGTH equals MAX_LENGTH and the error is raised. No, I doubt it, the type of the expression LEFT.DATA(1..LENGTH(LEFT)) & RIGHT is String, and the "&" is the standard string concatenation operator... Or please show me the parenthese I'm eventually missing ? ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: strange length check error 2018-01-23 16:41 ` Mehdi Saada @ 2018-01-24 16:22 ` Robert Eachus 0 siblings, 0 replies; 20+ messages in thread From: Robert Eachus @ 2018-01-24 16:22 UTC (permalink / raw) On Tuesday, January 23, 2018 at 11:41:58 AM UTC-5, Mehdi Saada wrote: > No, I doubt it, the type of the expression LEFT.DATA(1..LENGTH(LEFT)) & RIGHT is String, and the "&" is the standard string concatenation operator... Or please show me the parenthese I'm eventually missing ? Actually it is the double parenthesis after LENGTH(LEFT)) that I messed up. Tried to fix/delete the post almost immediately, but Google Groups was having none of it--wouldn't even let me post a correction. ^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2018-01-24 16:22 UTC | newest] Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2018-01-16 20:48 strange length check error Mehdi Saada 2018-01-17 1:42 ` Anh Vo 2018-01-17 9:23 ` Simon Wright 2018-01-17 12:13 ` Mehdi Saada 2018-01-17 13:19 ` AdaMagica 2018-01-17 13:41 ` Simon Wright 2018-01-17 21:37 ` Jeffrey R. Carter 2018-01-18 11:12 ` Simon Wright 2018-01-17 13:43 ` Mehdi Saada 2018-01-17 14:03 ` Mehdi Saada 2018-01-17 16:15 ` Mehdi Saada 2018-01-17 19:43 ` Simon Wright 2018-01-17 21:37 ` Mehdi Saada 2018-01-17 23:00 ` Niklas Holsti 2018-01-18 11:07 ` Simon Wright 2018-01-17 13:15 ` Simon Clubley 2018-01-17 8:49 ` Dmitry A. Kazakov 2018-01-23 16:18 ` Robert Eachus 2018-01-23 16:41 ` Mehdi Saada 2018-01-24 16:22 ` Robert Eachus
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox