comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@world.std.com>
Subject: Re: Language Lawyer question
Date: 2000/06/05
Date: 2000-06-05T00:00:00+00:00	[thread overview]
Message-ID: <wccr9acf6zv.fsf@world.std.com> (raw)
In-Reply-To: 8h97q3$68b$1@hobbes2.crc.com

"David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> writes:

> What is 0'Pred?

It depends on context, just like any other overload resolution
question.  If that were legal, then we could write:

    X: Integer;
    type Modular is mod 123;
    Y: Modular;

    X := (0)'Pred;
    Y := (0)'Pred;

and they would resolve to the appropriate type, just as if you had
written something like "X := -(0);"  Or, for that matter, "X := 0;".

In the rare cases where context doesn't determine the type already, you
use a qualified expression:

    if 0'Pred = -1 then -- Ambiguous; illegal.
    
    if Integer'((0)'Pred) = -1 then -- OK

    if (Integer'(0)'Pred) = -1 then -- OK
    
    if (0)'Pred = Integer'(-1) then -- OK

> 1.  Illegal if Natural
> 2.  -1 if Integer

Well, Natural and Integer are the same *type*, which is all overload
resolution cares about.  Also, Integer'Pred and Natural'Pred are the
same -- Natural'Pred(0) is legal, and does not raise any exception -- it
returns -1.  So, presumably if (0)'Pred were legal syntax, it would do
the same thing.

> 3,  2 ** modulus - 1 if modular
> 
> Now if you meant Some_Object'Pred -- that's possible
> 
> But, how would you do the equivalent of Modular_Type'Pred (0)
> with your syntax?

See above -- the same way you deal with overloaded operators applied to
literals.

To answer the original question: There is no very good reason why it's
T'Pred(X) rather than X'Pred.  The syntax could be redesigned to allow
attributes applied to expressions (although as Robert Eachus pointed out
in another note, it's tricky to get that right), and overload resolution
could be used in the normal way to resolve these things.  I suspect the
real reason is simply that the original language designers didn't like
that style -- a matter of taste.

Here's a related question:  Why can't I say:

    subtype Digit is range 0..9;

    function To_Character(X: Digit) return Character is
    begin
        return (String'("0123456789"))(X+1); -- Illegal.
    end To_Character;

?

- Bob




      parent reply	other threads:[~2000-06-05  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-06-02  0:00 Language Lawyer question Wes Groleau
2000-06-02  0:00 ` Paul Graham
2000-06-02  0:00 ` David C. Hoos, Sr.
2000-06-02  0:00   ` Keith Thompson
2000-06-04  0:00     ` Robert I. Eachus
2000-06-05  0:00       ` Robert A Duff
2000-06-05  0:00     ` Robert Dewar
2000-06-05  0:00   ` Robert A Duff [this message]
replies disabled

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