-- Illustrates a simple data type for Words -- A Word contains a String of some fixed maximum length -- and a current length with Ada.Text_IO; use Ada.Text_IO; with ada.Strings.Fixed; use ada.Strings.Fixed; with ada.Strings.Maps; use ada.Strings.Maps; with ada.Strings; use ada.Strings; procedure trySimpleWord is MaxWordLength: Integer := 80; type Word is record letters: String(1 .. MaxWordLength); length: Natural := 0; end record; WordTooLong: Exception; -- Compare two words function "="(x, y: Word) return Boolean is begin return x.letters(1..x.length) = y.letters(1..y.length) ; end "="; -- Output a Word procedure put(w: Word) is begin put(w.letters(1 .. w.length)); end put; procedure put_line(w: Word) is begin put(w.letters(1 .. w.length)); new_line; end put_line; -- Turn word w into a string function length(w: Word) return Natural is begin return w.length; end length; -- Turn word w into a string function toString(w: Word) return String is begin return w.letters(1 .. w.length); end toString; -- Turn string s into a word -- Raises exceptions if s is too long for a word function newWord(s: String) return Word is temp: Word; begin if s'length > MaxWordLength then raise WordTooLong; end if; temp.letters(1 .. s'length) := s(s'first .. s'last); temp.length := s'length; return temp; end newWord; w, x: Word; s: String := "Hi Mom!"; long: String(1 .. 300) := (others => 'x'); -- Long enough for 3 words begin w := newWord(s); x := newWord("Hi"); if w = x then put("Same"); else put("Different"); end if; put_line(w); put_line(x); put("Enter a word: "); get_line(long, len); w := newWord(long(1 .. len)); put_line(w); -- w := newWord(long); -- Causes an exception end trySimpleWord;