diff --git a/proposals/conditional_expressions/21-165.txt b/proposals/conditional_expressions/21-165.txt new file mode 100644 index 0000000..32e4f47 --- /dev/null +++ b/proposals/conditional_expressions/21-165.txt @@ -0,0 +1,56 @@ +To: J3 J3/21-165 +From: Ondrej Certik, Milan Curcic, Zach Jibben +Subject: Conditional expressions using intrinsic function +Date: 2021-June-27 +Reference: 21-157, 21-159 + +Paper 21-157 proposes two syntactic forms for conditional expressions. The +paper 21-159 proposes an alternative form. This paper proposes to implement +conditional expressions using an intrinsic function. + +The syntactic forms in 21-157 are too verbose, as argued in 21-159. The +form in 21-159 is concise, but based on the poll [1], it is not the most +popular either. + +We propose the following syntactic form + + is ifthen( predicate, consequent, alternative ) + +where the `predicate` is a scalar logical expression and the `consequent` +and `alternative` are compatible expressions. The `alternative` can be +another `ifthen` as shown in an example below. + +Examples: + + res = ifthen(x >= 0.0, sqrt(x), -0.0) + + res = ifthen(present(x), a, ifthen(present(b), b, 0)) + +Objections raised in the past: + +1) It is harder to do chaining due to the extra parenthesis at the end. + +Answer: it seems in practice it is not too bad, as in the second example +above. Here is a longer chain: + + res = ifthen(present(x), x, ifthen(present(y), y, + ifthen(present(z), z, 0))) + +2) `ifthen` does not behave like a regular function, it is effectively new +syntax, so we might as well actually introduce new syntax, to make it less +confusing + +There is a precedent in optional arguments, where the argument is not +evaluated but passed through through nested functions until it hits the +function `present`. In a similar way, the arguments to `ifthen` are not +evaluated ahead of time, but rather passed into the function `ifthen` +itself, similar to how `present(x)` works. Both `ifthen` and `present` must +be implemented by the compiler itself, they cannot be implemented in +standard Fortran code. + +References: + +[1] https://fortran-lang.discourse.group/t/ + poll-fortran-202x-conditional-expressions-syntax/1425 + +