comp.lang.ada
 help / color / mirror / Atom feed
From: adambeneschan@gmail.com
Subject: Re: character literals
Date: Tue, 11 Feb 2014 15:56:52 -0800 (PST)
Date: 2014-02-11T15:56:52-08:00	[thread overview]
Message-ID: <9d08b5d0-012c-4b94-b8b9-ea6e83f2df4f@googlegroups.com> (raw)
In-Reply-To: <rq8lf9tqg08bv32ouaudvsl62jto1abpu7@4ax.com>

On Tuesday, February 11, 2014 2:27:57 PM UTC-8, ag...@drrob1.com wrote:
> I have been having a difficulty in my code with character literals.
> 
> For example
> 
>  IF ch in '0' .. '9' THEN
> 
> This gives an error because the compiler is complaining that it does
> not know if I mean a character, wide_character, or wide_wide_character
> for my literals.  How do I indicate which I want?

I don't think your compiler should not be giving an "ambiguous" error on the above IF statement.  The literals '0' and '9' are indeed ambiguous, because they are actually overloaded functions that could return Character, Wide_Character, or Wide_Wide_Character, or possibly one or more user-defined enumeration types; however, assuming "ch" is a variable, it will have a known type, and I think that type can be used to resolve the types of the overloaded range.  However, on checking the language rules, it's not 100% clear to me that that's the case.  Neither compiler I tried this on reports an error, though.

This is different, though:

    for Ch in '0' .. '9' loop

because this loop statement *is* the declaration of Ch, so the compiler has to be able to resolve the type just from the literals '0' and '9', and it can't.  However, this is legal:

    Start_Ch : Character;

    for Ch in Start_Ch .. '9' loop

because now although '9' is ambiguous, the language will use the type of Start_Ch to resolve the type of '9'.  I don't think it's necessary (even from a style standpoint) to include the type name in the "for" statement; others may differ.

The first "loop" statement, which is ambiguous, was legal in Ada 83, when there was only one character type; when Wide_Character was added to Ada 95 [Wide_Wide_Character wasn't added until Ada 2005], this became illegal, which caused some compatibility headaches for existing code.  

Also:

    type Traffic_Light is (Red, Yellow, Green);
    type RGB is (Red, Green, Blue);

    for Color in Red .. Green loop    -- ambiguous, illegal
    for Color in Green .. Blue loop   -- legal, since there is only one meaning
                                      -- of Blue

However, I'd definitely recommend including the type name in a case like this.

    for Color in RGB range Green .. Blue loop

Finally, the language does have one special rule:

    for I in 0 .. 9 loop

The literals 0 and 9 could be resolved to any integer type, which would make this ambiguous since there are normally multiple integer types visible in the program (Integer, Long_Integer, Short_Integer, maybe types in Interfaces if you "use" that packaged).  But the language rules decree that the type will be Integer in that case.  This is a situation where some programmers might recommend making the type Integer explicit.

                                -- Adam


  parent reply	other threads:[~2014-02-11 23:56 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-11 22:27 character literals agent
2014-02-11 22:49 ` J-P. Rosen
2014-02-11 23:45   ` Bill Findlay
2014-02-11 23:49     ` Ludovic Brenta
2014-02-11 23:58       ` adambeneschan
2014-02-11 23:56 ` adambeneschan [this message]
2014-02-12  0:18   ` adambeneschan
2014-02-12  1:34     ` agent
2014-02-12  2:03       ` adambeneschan
2014-02-12 12:50         ` agent
2014-02-12  1:30   ` Jeffrey Carter
2014-02-12  1:50     ` adambeneschan
2014-02-12 15:53   ` Robert A Duff
2014-02-12 17:55     ` J-P. Rosen
2014-02-14 12:39       ` agent
2014-02-14 18:36         ` AdaControl was: " Simon Clubley
2014-02-15  6:26         ` J-P. Rosen
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox