Frank Heckenbach a écrit :
Hi!
First of all: Happy New Millennium! :-)
Peter and I are discussing the possibility of including some kind of conditional operator, like `foo ? bar : baz' in C. Since GPC uses GCC's backend, this should be easy to do semantically, the main question is about syntax:
?: like in C -- the symbols wouldn't conflict AFAICS, but it seems to me quite un-Pascalish...
if foo then bar else baz (i.e., the same syntax as conditional statements, just with expressions, e.g. `x := if cond then 42 else 17'). Possible problems:
dangling else (probably not since each conditional operator will be required to have an else clause)
conflicts with `if' statements when nested into them (maybe not either for the same reason)
issues with precedence (compared to other operators) and associativity (i.e., how nested conditional operators will be interpreted) -- C has the same issues and resolves them with some rules, but like all such issues, they can confuse programmers sometimes (think of precedence of `and' and `or')...
if foo then bar else baz endif (or `fi' instead of `endif') -- this would avoid all the problems and make the syntax somewhat clearer I think...
if foo then bar else baz end -- the same, but without introducing a new reserved word (of which there are already way too many), and `end' is unambiguous in this context AFAICS.
Peter seems to remember that other Pascal compilers and related languages have something like this. Does someone know anything about these? Maybe we don't have to reinvent the wheel...
Indeed, it was already in Algol60, the first ancestor of Pascal. The syntax was as above
x := if cond then 42 else 17
and I had some trouble in beginning with Pascal to give away this very natural syntax !
No dangling else, use of () to be sure in case of doubt about precedences would be the simplest (but I have an historical bias !)
Hope this helps
Maurice