|
| 1 | +To: J3 J3/21-165 |
| 2 | +From: Ondrej Certik, Milan Curcic, Zach Jibben |
| 3 | +Subject: Conditional expressions using intrinsic function |
| 4 | +Date: 2021-June-27 |
| 5 | +Reference: 21-157, 21-159 |
| 6 | + |
| 7 | +Paper 21-157 proposes two syntactic forms for conditional expressions. The |
| 8 | +paper 21-159 proposes an alternative form. This paper proposes to implement |
| 9 | +conditional expressions using an intrinsic function. |
| 10 | + |
| 11 | +The syntactic forms in 21-157 are too verbose, as argued in 21-159. The |
| 12 | +form in 21-159 is concise, but based on the poll [1], it is not the most |
| 13 | +popular either. |
| 14 | + |
| 15 | +We propose the following syntactic form |
| 16 | + |
| 17 | + <cond-expr> is ifthen( predicate, consequent, alternative ) |
| 18 | + |
| 19 | +where the `predicate` is a scalar logical expression and the `consequent` |
| 20 | +and `alternative` are compatible expressions. The `alternative` can be |
| 21 | +another `ifthen` as shown in an example below. |
| 22 | + |
| 23 | +Examples: |
| 24 | + |
| 25 | + res = ifthen(x >= 0.0, sqrt(x), -0.0) |
| 26 | + |
| 27 | + res = ifthen(present(x), a, ifthen(present(b), b, 0)) |
| 28 | + |
| 29 | +Objections raised in the past: |
| 30 | + |
| 31 | +1) It is harder to do chaining due to the extra parenthesis at the end. |
| 32 | + |
| 33 | +Answer: it seems in practice it is not too bad, as in the second example |
| 34 | +above. Here is a longer chain: |
| 35 | + |
| 36 | + res = ifthen(present(x), x, ifthen(present(y), y, |
| 37 | + ifthen(present(z), z, 0))) |
| 38 | + |
| 39 | +2) `ifthen` does not behave like a regular function, it is effectively new |
| 40 | +syntax, so we might as well actually introduce new syntax, to make it less |
| 41 | +confusing |
| 42 | + |
| 43 | +There is a precedent in optional arguments, where the argument is not |
| 44 | +evaluated but passed through through nested functions until it hits the |
| 45 | +function `present`. In a similar way, the arguments to `ifthen` are not |
| 46 | +evaluated ahead of time, but rather passed into the function `ifthen` |
| 47 | +itself, similar to how `present(x)` works. Both `ifthen` and `present` must |
| 48 | +be implemented by the compiler itself, they cannot be implemented in |
| 49 | +standard Fortran code. |
| 50 | + |
| 51 | +References: |
| 52 | + |
| 53 | +[1] https://fortran-lang.discourse.group/t/ |
| 54 | + poll-fortran-202x-conditional-expressions-syntax/1425 |
| 55 | + |
| 56 | + |
0 commit comments