-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix the syntax of if and while #9746
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
Comments
So just to be on the same page, let's formalize that:
A few examples from dotty repl: Statement with
Statement with
Statement with
@smarter what 'cond' do you mean in |
None of these examples should work. |
That was quick 👍 docs might need to be updated: https://dotty.epfl.ch/docs/reference/other-new-features/control-syntax.html |
I too read the docs recently and liked thenless indented block syntax. In fact, I would also like doless indented block syntax for while.
Full disclosure, last week I wrote one multiline if and used then. Yesterday I wrote only one-liners using then. I wrote no while, only recursive methods. I agree that it's the other syntax that is problematic, which we should call "legacy parens". In fact, I'm not sure what work the parser is doing, but the only problem seems to be that it doesn't like an indented condition, which IMHO ought to be (easily) supported:
It seems to me that accepting the indented condition solves all these problems.
In sum, it shouldn't matter if the condition begins on the next line, and while should accept optional do the same way if accepts optional then, when followed by an indent block. |
I really dislike This issue has demonstrated that if x < 10 then
println(x)
x += 1
if
(x < 10) && true then
x += 1
if
val tmp = ...
tmp > 0
then
... |
I did not consider the third example, a braceless indented block for the condition. The compelling example is the block condition for while in https://dotty.epfl.ch/docs/reference/dropped-features/do-while.html It says do-while was rare, and therefore block condition for this usage is rare, so it's not terrible to require braces. The argument for discouraging block condition is that it's harder to read. Is it terrible to require then and do always when most conditions are one-liners? I don't have very strong opinions about this syntax because I'm suspending judgment while I gain more experience. It's possible "optional then" and "support legacy parens with arbitrary indents" are orthogonal issues. |
Potentially worth taking into account as it is about if syntax: #9790 |
There are three choices here to make for the interplay with indentation and ifs:
My tendency would be to go with (2). |
Fix #9746: Require `then` in new conditional syntax
@odersky @anatoliykmetyuk Just to be sure, are we keeping it at (2) or at (1)? I see there is still things like https://github.com/lampepfl/dotty/blob/5274c48767c655bcf9d994f649b43f0e7214dde9/compiler/src/dotty/tools/dotc/typer/Checking.scala#L479-L481 so I am guessing it's (2) ? |
And isn't this going against what Dotty is trying to do? By which I mean we want reduce the number of ways to do the same thing. We removed the optional |
This compiles but shouldn't (we should require
then
when using indentation syntax)if cond fooA() fooB()
This compiles in Scala 2.13 but not in Dotty, but we should be able to support it:
/cc @kpbochenek
The text was updated successfully, but these errors were encountered: