1

Take the following example:

   type MyArray is array(1..10) of Float;
   type MyRecord is record
      a: Float;
      b: Integer;
      c: Boolean;
      d: MyArray;
   end record;
   …
   function foo(x: MyRecord) return MyRecord is
     y: MyRecord := x;
   begin
     y.b = -1;
     return y.b;
   end foo;

It would be nice if I could do something like this instead with foo:

   function foo(x: MyRecord) return MyRecord is
     (x with (b => -1));

Where the simpler notation (which is not valid Ada) allows me to write foo as an expression function. Is there a way to do something like this in Ada?

Ben Hocking
  • 7,790
  • 5
  • 37
  • 52

1 Answers1

8

Well, Ada202x is working on Delta Aggregates which would be what you want essentially. Until then the only 2 options I could think of were:

Plain old aggregates:

function foo1(x : MyRecord) return MyRecord is
    (a => x.a, b => -1, c => x.c, d => x.d);

Extended Return:

function foo2(x : MyRecord) return MyRecord is
begin
    return Result : MyRecord := x do
        Result.b := -1;
    end return;
end foo2;

I know extended return doesn't get you the expression function you wanted, but it is a nicer syntax and can be used with limited types in interesting ways. Right now you will have to rely on plain old aggregate syntax.

Jere
  • 3,124
  • 7
  • 15