There's so many reactions :-), so let me do a mass-reply...
Clive Rodgers wrote:
Maurice Lombardi a ecrit :
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
As a native Algol60 speaker, I agree - this was one of the features I too missed when Algol60 faded away. Syntax is close to natural language (easy to teach, easy to learn, and a clean syntax - that is the fundamental principle of Pascal). The rules of precedence for Algol60 are basically evaluate from left to right unless specific rules apply - isn't that already the case for Pascal?
I think you can express it this way. I'd have said: Use operator precedence and if it's the same, use their associativity which is left for most operators and none for relational operators (i.e. `a = b = c' is invalid), but it seems to result in the same...
I don't think we need anything else. The endif (or end) construction is un-Pascalish - use parentheses if required, in the same way as begin/end is used when needed for conditional statements.
Probably. I think `if' without a terminator was basically a mistake (if only because of dangling else), and some later languages have corrected it, but it seems we have to live with it, also in this case...
However, why restrict it to the RHS? ISTR that in Algol68 (or was it SCL, the ICL VME operating system language) you could say:
if cond1 then a else b := if cond2 then expr1 else expr2
GCC has such an extension (over the C standard) for the ?: operator. I don't think the need for it is as big as on the RHS (though I'd probably find some places in my code where it could be used). But (at least when done straightforwardly), this syntax causes some parsing conflicts (with `if' statements, I assume), and might also be difficult to read because until reading the `else' it's not clear that this is no `if' statement.
or something like that, which could lead for example to (I think in SCL) the following curious-looking statement (until you worked out what it was doing):
if if then then else else := if not then while else for
Nice. :-)
since there were no reserved words (the compiler being sufficiently clever to work out when to expect one!). Fun to read anyway!
Well, GPC is doing something like this for words that are reserved only in some dialects (e.g., value). But these words above are reserved in all Pascal dialects, so GPC treats them as reserved unconditionally, and I think it should remain that way... ;-)
Tom Verhoeff wrote:
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:
I would like to propose the following syntax:
<expr> if <cond> else <expr>
This is concise and nice (you can view "if <cond> else" as infix operator). Disadvantage: it requires priorities/parentheses for proper binding, it uses the if/else, which also appears in the conditional statement (this is also an advantage, since it does not require new reserved words/symbols).
This doesn't seem to cause parsing conflicts, AFAICS. However, it's quite different from `if' statements (and therefore maybe confusing), so I'd tend to favour the if-then-else version...
However, this syntax seems to work on the LHS without conflicts, so the following seems possible:
a if cond else b := c if cond2 else d
So, I'd suggest either this syntax on both sides, or the syntax above only on the RHS...
Prof. Abimbola Olowofoyeku (The African Chie wrote: ^ (BTW, you have an `f)' missing there in your `From:' line.)
I have read the various comments about the Algol60 syntax - that is as good as any other. But like one of the other respondents, I am a bit concerned about compatibility with other Pascal compilers. This can of course be tempered by making sure that the new thing is rejected when other a switch for any other "standard" (e.g., BP, Delphi, EP, etc) is used, or perhaps, better still, to require a {$gnu-pascal} directive before it can be used. Does this make sense?
Yes (the former). It should be rejected/warned about when any dialect option is given. {$gnu-pascal}, however, just turns off all dialect options which is also the default, so requiring {$gnu-pascal} would be unsystematic/impossible...
Frank