comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: condition true or false? ->  (-1 < sizeof("test"))
Date: Mon, 21 May 2012 08:45:41 -0700 (PDT)
Date: 2012-05-21T08:45:41-07:00	[thread overview]
Message-ID: <49eaf626-d504-472c-ae3b-cdb78595a82c@googlegroups.com> (raw)
In-Reply-To: <00240c6dd26962f50d5c57a933c137ef@dizum.com>

On Monday, May 21, 2012 8:28:44 AM UTC-7, Nomen Nescio wrote:
> Forwarding this to guys who write code in real languages to see what they
> think of this. AFAIK you cannot get something like that past the compiler in
> Ada...and you would have to define a type or a subtype to even have an
> unsigned int unless you use a modular type IIRC.

The last part is pretty much correct.  If you do something like this in Ada:
 
   B := -1 < Some_Type'Size;

where B is a Boolean, it works like this: Some_Type'Size is a universal integer, and it can be converted to any integer type.  However, in a case like this where there's no variable or anything else that tells the language which type to use, the language has a preference for "root_integer", which is a signed integer type.  So the above would do what you expect (i.e. set B to True since 'Size can never be negative).  (The rule about preferring root_integer was added in Ada 95.  I don't know for sure whether the above is legal in Ada 83; I think this might have been an error.)

You could create a situation where -1 is treated as a modular type expression, so that the above would set B to False.  However, you really have to try.  For instance:

    type Word is mod 2**32;

    B := -1 < Word' (Some_Type'Size);

would cause Some_Type'Size to be interpreted as a Word, and then the - and < operators are treated as the ones that operate on Word.  Or:

    One : constant Word := 1;

    B := -One < Some_Type'Size;

But you do have to have some modular type involved to get False for B, and you have to either use the type name in the expression, use the name of an object of that type, or do something like

    B := Thing_Defining_Modular_Type."<" (-1, Some_Type'Size);

It won't happen implicitly.

                           -- Adam



  parent reply	other threads:[~2012-05-21 15:45 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <95634f38f6ee0d116da523fdc2c9f5ca@dizum.com>
2012-05-21 15:28 ` condition true or false? -> (-1 < sizeof("test")) Nomen Nescio
2012-05-21 15:37   ` Richard Maine
2012-05-21 15:45   ` Adam Beneschan [this message]
2012-05-21 17:16     ` Robert A Duff
2012-05-22  1:08   ` Terence
2012-05-22  7:54     ` Les Neilson
2012-05-22 23:19       ` Terence
2012-05-23  7:20         ` Les Neilson
2012-05-22 15:43     ` Fritz Wuehler
2012-05-22  1:45   ` glen herrmannsfeldt
2012-05-22  6:11     ` Nomen Nescio
2012-05-22  8:00       ` Martin
2012-05-22  9:55       ` BartC
2012-05-22 12:07         ` Dmitry A. Kazakov
2012-05-22 16:25           ` BartC
2012-05-22 17:03             ` Dmitry A. Kazakov
2012-05-22 17:26               ` BartC
2012-05-22 18:18                 ` Dmitry A. Kazakov
2012-05-23  7:26                   ` Terence
2012-05-22 17:56         ` Fritz Wuehler
2012-05-22 10:29     ` Georg Bauhaus
2012-05-30  3:22   ` robin.vowels
replies disabled

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