AdaCore / ada-spark-rfcs

Platform to submit RFCs for the Ada & SPARK languages
63 stars 28 forks source link

[RFC] Conditional return #31

Closed jklmnn closed 3 years ago

jklmnn commented 4 years ago

Conditional return statement similar to conditional exits in loops. Full text: https://github.com/jklmnn/ada-spark-rfcs/blob/conditional_return/considered/rfc-conditional-return.rst

yannickmoy commented 4 years ago

It seems to me that your proposed one-line syntax:

return when not Success;

can already be expressed on one line in Ada:

if not Success then return; end if;

so maybe the main obstacle here is your habit of putting end if on a separate line?

senier commented 4 years ago

Fair enough. On the other hand, loop exit statements could be expressed as if not Success then exit; end if', too. The Ada 83 Rationale states:

The redundancy provided for conditional exits is itself motivated by readability considerations: loop termination conditions should be marked very conspicuously. Thus, in the recommended paragraphing,

exit when CONDITION;

is certainly more conspicuous than the equivalent form in which the exit statement is nested within an if statement:

if CONDITION then
  exit;
end if;

I wonder why "loop termination conditions should be marked very conspicuously" and why the same argument shouldn't also apply to subprogram returns?

mosteo commented 4 years ago

There are some advocates of having only one return per subprogram, at the end. If you're going to break this rule, having a 'conspicuous' way of doing it is better. In that case maybe it could be used also for functions:

return <expr> when <cond>;

yannickmoy commented 4 years ago

@mosteo the RFC also proposes to have it for functions, with the syntax you propose.

@senier good archeological work! you've got a point here, indeed the same should hold for termination of loop and subprogram.

AdaDoom3 commented 3 years ago

A new RFC encompassing and expanding on this proposal has been added - so I am going to move to close this issue. Any new commentary should be put under this RFC:

https://github.com/AdaCore/ada-spark-rfcs/pull/73