Open eed3si9n opened 3 years ago
Output of -Xprint:typer
:
package <empty> {
class Test() extends Object() {
{
def $anonfun(body: => Any): Unit = this.test("hello")(body)
closure($anonfun)
}
():Unit
def test(name: String)(body: => Any): Unit = ()
}
}
So the warning is correct, test("hello")
gets expanded to a lambda which is a pure expression. I guess you expected the assert to be a block argument because it's indented, but indentation syntax does not work that way: https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html
I guess you expected the assert to be a block argument because it's indented, but indentation syntax does not work that way
Could we please catch these in the parser like the follows:
test("hello")
| assert(1 == 1)
| ^^^^^^^^^^^^^^
🚩 Off-side rule violation. An indentation is started but the previous line does not end with a marker.
I don't know enough about the parser to say. Somewhat related: https://github.com/lampepfl/dotty/issues/10372
Should I open a new issue, or can I reuse this issue with a different title?
As you wish.
There are valid situations where one might want to indent some code, for instance to express a hierarchy of definitions (see Definitions.scala
where this scheme is used). Or, what about this one:
def test(y: Int, z : Int) =
val x = 10
x
+ y
+ z
That seems like it should be supported as well.
There are valid situations where one might want to indent some code
The test
example on Python would error as follows:
>>> def test(x, y):
... x = 10
... x
File "<stdin>", line 3
x
IndentationError: unexpected indent
If we're saying that indentation has semantics of { ... }
, then my expectation is that the indentation must now be both necessary and sufficient with regard to starting a new block or a clause, and cannot have any other soft meanings Scala 2 may have admitted. Otherwise, it's misleading.
class Test:
test("hello")
assert(1 == 1)
I ran into this while writing a unit test using MUnit. If Guillaume didn't show me -Xprint:typer
I wouldn't have guessed what was happening.
To avoid breaking existing brace-style code, do you think it's possible to employ something like:
?
// ok
def test(y: Int, z : Int) = {
val x = 10
x
+ y
+ z
}
// not ok
def test(y: Int, z : Int) =
val x = 10
x
+ y
+ z
I have a draft PR for this https://github.com/lampepfl/dotty/pull/10691.
Minimized code
compile:
or more simply:
Output
Expectation
Same as putting braces, no warnings should be printed.