• Beginners question

    From uq5huoixplph@gmail.com@21:1/5 to All on Fri Nov 12 06:32:48 2021
    Hi,

    I'm beginning to play with Ada, and run into this

    with Ada.Text_IO; use Ada.Text_IO;
    procedure Learn is
    subtype Alphabet is Character range 'A' .. 'Z';
    begin
    Put_Line ("Learning Ada from " & Alphabet'First & " to " & Alphabet'Last); end Learn;

    Now I want to play a bit with the code ... and this fails and I don't get why and how to solve.

    Put_Line ("Learning Ada from " & Alphabet'First & " to " & Alphabet'Last-1);
    or
    Put_Line ("Learning Ada from " & Alphabet'First & " to " & (Alphabet'Last-1));

    Anyone more expirienced can explain this to my, please?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From uq5huoixplph@gmail.com@21:1/5 to Niklas Holsti on Fri Nov 12 07:44:02 2021
    Niklas Holsti schrieb am Freitag, 12. November 2021 um 16:24:20 UTC+1:
    On 2021-11-12 16:32, uq5huo...@gmail.com wrote:
    Hi,

    I'm beginning to play with Ada, and run into this

    with Ada.Text_IO; use Ada.Text_IO;
    procedure Learn is
    subtype Alphabet is Character range 'A' .. 'Z';
    begin
    Put_Line ("Learning Ada from " & Alphabet'First & " to " & Alphabet'Last); end Learn;

    Now I want to play a bit with the code ... and this fails and I don't get why and how to solve.

    Put_Line ("Learning Ada from " & Alphabet'First & " to " & Alphabet'Last-1);
    or
    Put_Line ("Learning Ada from " & Alphabet'First & " to " & (Alphabet'Last-1));

    Anyone more expirienced can explain this to my, please?
    The Character type is not an integer type (unlike "char" in C), so you
    cannot subtract 1 from a Character (or an Alphabet value). Character is
    an enumeration type in Ada.

    To get the Alphabet value immediately preceding Alphabet'Last, use the
    Pred ("predecessor") attribute function:

    Alphabet'Pred (Alphabet'Last)

    If you need to take larger strides in the Alphabet enumeration, you can
    use the Pos and Val attribute functions. Pos returns the "position
    number" of its argument, and that is an integer. Val performs the
    opposite mapping. For example, to get the Alphabet value that is three positions before Alphabet'Last, you can do

    Alphabet'Val (Alphabet'Pos (Alphabet'Last) - 3)

    Dear Niklas,
    thanks for the explanation. So I run into the strong typing and Ada prevents me to programm C-like. I really like that. :-)
    I will read more onto the attributes to get familar with this.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Niklas Holsti@21:1/5 to uq5huo...@gmail.com on Fri Nov 12 17:24:17 2021
    On 2021-11-12 16:32, uq5huo...@gmail.com wrote:
    Hi,

    I'm beginning to play with Ada, and run into this

    with Ada.Text_IO; use Ada.Text_IO;
    procedure Learn is
    subtype Alphabet is Character range 'A' .. 'Z';
    begin
    Put_Line ("Learning Ada from " & Alphabet'First & " to " & Alphabet'Last);
    end Learn;

    Now I want to play a bit with the code ... and this fails and I don't get why and how to solve.

    Put_Line ("Learning Ada from " & Alphabet'First & " to " & Alphabet'Last-1); or
    Put_Line ("Learning Ada from " & Alphabet'First & " to " & (Alphabet'Last-1));

    Anyone more expirienced can explain this to my, please?


    The Character type is not an integer type (unlike "char" in C), so you
    cannot subtract 1 from a Character (or an Alphabet value). Character is
    an enumeration type in Ada.

    To get the Alphabet value immediately preceding Alphabet'Last, use the
    Pred ("predecessor") attribute function:

    Alphabet'Pred (Alphabet'Last)

    If you need to take larger strides in the Alphabet enumeration, you can
    use the Pos and Val attribute functions. Pos returns the "position
    number" of its argument, and that is an integer. Val performs the
    opposite mapping. For example, to get the Alphabet value that is three positions before Alphabet'Last, you can do

    Alphabet'Val (Alphabet'Pos (Alphabet'Last) - 3)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From uq5huoixplph@gmail.com@21:1/5 to All on Fri Nov 12 07:44:56 2021
    Dear Niklas,
    thanks for the explanation. So I run into the strong typing and Ada prevents me to programm C-like. I really like that. :-)
    I will read more onto the attributes to get familar with this.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)