comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Ada needs some modernization
Date: Tue, 31 May 2022 21:55:05 +0200	[thread overview]
Message-ID: <t75rqo$k0k$1@gioia.aioe.org> (raw)
In-Reply-To: 75d90749-242f-42b8-ba0b-299f7ac693e0n@googlegroups.com

On 2022-05-31 19:54, Matt Borchers wrote:
> Throughout my career, I often find myself writing code similar to:
> 
> if (A and B) or else (not A and C) then...
> 
> and I always wished there was a better and clearer way to write this in Ada.  Then along came if expressions.  But, if expressions don't help that much with readablity although it is arguably simpler:
> 
> if (if A then B else C) then...

Not same. In the former A may be computed twice.

> What amendment can we suggest to the Ada syntax so the if expression be better written when used in an if statement?

I newer felt it necessary. To me much more aggravating is code that 
combines test/allocator with renaming, i.e.

    if P /= null then
       declare
          X : T renames P.all;
       begin
          ...
       end;
    end if;
------------
    if X in T'Class then
       declare
          XX : T'Class renames T'Class (X);
       begin
          ...
       end;
    end if;
-----------
    P : access T'Class := new S;
    X : S renames S (P.all);

If one could come up with some syntax for if-then-declare and 
new-then-declare that would cover a lot of cases.

> I know other languages support this and it often looks like  A ? B : C  or something similar.  That's certainly not Ada-like IMO, but I can't think of something better. These same languages often also have a null check operator  A ?? B  (where A and B are access types of the the same Type) such that if A is not null then A is returned otherwise B is returned.  So useful and helpful!

Not in a strongly typed language IMO.

[...]

> Maybe something like:
> 
> for i in arr'Range loop
>      if arr(i) = match then
>          --do something A
>          exit;
>      end if;
> then
>      --do something else B
> end loop;

I usually use a nested function, e.g. search with a fallback:

    function Get_Me_Something return Element is
    begin
       for I in arr'Range loop
          if Arr (I) = match then
             return Arr (I);
          end if;
       end loop;
       return Defaul;
    end Get_Me_Something

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  parent reply	other threads:[~2022-05-31 19:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-31 17:54 Ada needs some modernization Matt Borchers
2022-05-31 19:05 ` Gautier write-only address
2022-05-31 19:55 ` Dmitry A. Kazakov [this message]
2022-05-31 22:46 ` Randy Brukardt
2022-06-01  7:24   ` John McCabe
2022-06-01 19:00 ` Jeffrey R.Carter
2022-06-02  5:56 ` G.B.
2022-06-10 16:38 ` Brad Moore
replies disabled

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