Re: ADA Popularity Discussion Request

From: Chad R. Meiners (chad.rmeiners_at_gmail.com)
Date: 09/09/04


Date: 8 Sep 2004 22:38:00 -0700


"jayessay" <nospam@foo.com> wrote in message
news:m3acw0bl1u.fsf@rigel.goldenthreadtech.com...
> with Text_Io; use Text_Io;
>
> procedure Foo is
>
> type Hex_Num is mod 16;
>
> function Hext (N : Integer) return Hex_Num is
> begin
> -- == (1+ (n mod 16)), which is the logic error.
> return Hex_Num(1 + (N mod Hex_Num'Modulus));
> end Hext;

However, most Ada programmers would have written it like

procedure Foo is
    type Hex_Num is mod 16;

    function To_Hex_Num(Item : Integer) return Hex_Num is
    begin -- This function provides the correct conversion
           -- for use by all other subroutines.
        return Hex_Num(Item mod Hex_Num'Modulus);
    end To_Hex_Num;

    function Hext (N : Integer) return Hex_Num is
    begin
        return 1 + To_Hex_Num(N); -- Note To_Hex_Num(1+N) also works
    end Hext;

    ...

and avoided the problem altogether.

-CRM