-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
If Without Else #234
Comments
I like the proposal! It’s both intuitive and unambiguous. I wonder if we should support |
I included the else without then for parity with XSLT. I'm happy leaving that bit out if the consensus is that it is not needed. |
Good proposal, @rhdunn ! Just this: I also think that |
good ideas here. Agree else by itself prolly bad - more likely to be a mistake than intended. As a side not, an ifless then or else should remain an error for the same reason - to catch mistakes. |
My first instinct was that this is imaginative, and I quite like it. But I'm concerned about this:
because its meaning is not at all what a C or Java programmer would expect: I think this would become a fruitful source of bugs. As an alternative, how about abandoning the ternary conditional proposed in issue #171, and using
|
Another possibility would be
|
I assume that’s similar with our existing syntax: if (exists($e/@one)) return $e/@one,
if (exists($e/@two)) return $e/@two Indeed, newcomers will never understand why What about enforcing the first parenthesis if no
Examples: (: legal :)
if(A) then B else C
if(A) then (B)
if(A) then (if(B) then (C))
(: illegal :)
if(A) then B
if(A) then if(B) then (C) In general, I’m still wondering why the dangling else problem is really such a showstopper, as there are various other implicit rules that developers need to understand. For example, users need to know about precedence rules to understand how Developers have become used to this in so many other languages. Why do we expect them to be overwhelmed when we introduce it in our language? |
Regarding "dangling else", I seem to recall the WG consensus was "just because other programming languages made this mistake doesn't mean that we need to make it too". (Getting the if-then-else syntax agreed was one of Mary Fernandez' major triumphs, by the way. The arguments were very heated. Parking the dangling else problem, leaving that fight to another day, was the best way of resolving the issue at the time.) I don't think the problem is that users will be overwhelmed. The problem is that they will make mistakes through carelessness, as they do in other languages. Personally, I avoid the problem in Java by always using curly braces. I think it's better language design if good coding practice is mandatory wherever possible. The requirement to use parens feels rather clumsy to me. It seems to break an orthogonality principle, though I find it hard to identify exactly why it makes me nervous. I prefer my curly-braces proposal. |
Thanks for the insight. As often, it feels obstructive and liberating at the same time not to be loaded with the historical context. I like the curly braces, and the syntax is compact and well-known. With my latest proposal, I agree it would be tiresome to explain why the additional |
One could also allow:
where $x return ...
|
This has been resolved. We now have |
This is based on the discussions in today's QT4CG meeting, as suggested by @dnovatchev.
Use Case
It is common to have an if condition where the
else
branch does nothing (i.e. yields the empty sequence). In BaseX, this requirement has lead to them making the else branch optional.It can also be conceivable to want to elide the
then
branch as a way of not negating the if condition expression.In the XSLT 4.0 draft, both of these are possible in the changes to support optional
@then
(or possibly@select
) and@else
attributes. Part of this is due to requiring backward compatibility with XSLT 3.0 that only allows the then branch within child elements. It would be nice to be able to support this in XPath and XQuery for parity between the languages.Design
The rationale for not allowing an optional
else
is to avoid the dangling else problem from C and other languages that have optional else statements. That is:is ambiguous as the else could be part of the
$d
if statement or the$c
if statement. This would require parenthesis around the if statement to resolve the ambiguity, but the syntax should not require that. -- That is, it should be clear to the reader what the if statement will do.As such, I propose the following variants:
(1) An if statement with a then and else expression -- currently supported:
(2) An if statement with an else expression, but not a then expression -- new, should be unambiguous:
(3) An if statement with a then expression, but not an else expression -- to resolve the dangling else, I propose to use
return
instead ofthen
:This works analogously to the existing FLWOR, switch, typeswitch and other expressions that use
return
to denote the return/result expression.For the dangling else, both cases are clear:
(a) when the
$c
(outer) if has the elided else expression:(b) when the
$d
(inner) if has the elided else expression:Syntax
Replace:
with:
Design note: There is a
ReturnClause
symbol that is defined as"return" ExprSingle
. Because of this, and to indicate that thethen
andelse
parts are not expressions, I've opted for the term clause. -- This matches the use in FLWORExpr, SwitchExpr, TypeswitchExpr, etc. that all use the term clause. I've also separated them out to make theIfExpr
more readable now that it has optional parts.Semantics
Replace:
with:
Those should be the only required changes.
The text was updated successfully, but these errors were encountered: