question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange behaviour in repl when using fewerBraces, first it does not work then it does

See original GitHub issue

Compiler version

Scala 3.1.3-RC1-bin-SNAPSHOT-nonbootstrapped-git-ccd39b7 using repl in sbt from current dotty repo.

Minimized code and output

Welcome to Scala 3.1.3-RC1-bin-SNAPSHOT-nonbootstrapped-git-ccd39b7 (11.0.13, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                               
scala> import language.experimental.fewerBraces
                                                                                                                               
scala> Seq(1,2,3).apply:   // press Alt+Enter to get new line
     |   1
-- Error: -----------  // this should work and not give this error
1 |Seq(1,2,3).apply:   
  |^^^^^^^^^^^^^^^^
  |missing arguments for method apply in trait SeqOps
1 error found
                                                                                                                               
scala> Seq(1,2,3).apply:  // don't press Alt+Enter
val res0: Int => Int = Lambda$14686/0x000000080384a040@44471878
                                                                                                                               
scala> Seq(1,2,3).apply:  // press Alt+Enter to get new line
     |   1
     | 
val res1: Int = 2   // now it suddenly works as should
                                                                                                                               
scala> 

Expectation

The first try above should work. Now it first does not work as expected; then it starts working after creating the first lambda by providing an empty arg.

Maybe it is cased by some kind of jline parsing issue?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
griggtcommented, Feb 15, 2022

So, the issue has to do with how import statements from previous REPL lines are brought into the compiler context.

This happens here:

https://github.com/lampepfl/dotty/blob/1476a30b7949d2078aa6ff6660b54774cdeb2f00/compiler/src/dotty/tools/repl/ReplCompiler.scala#L41-L66

where the user defined imports from previous lines are folded into the rootContext for the new compiler run.

However, the REPL is special in that the Parser is run standalone rather than as part of the frontend phases of the compiler, which in turn means that the REPL parser is not seeing an up-to-date context since the next run of the compiler has not yet started.

scala> import language.experimental.fewerBraces

scala> "hello"
val res0: String = hello

scala> Seq(7,8,9).apply:
     |   1
     | 
val res1: Int = 8

The issue does not manifest in the example above because the import is brought into context during compilation of the line containing “hello”. Indeed the issue is only present when the code on the REPL line immediately following the fewerBraces import requires fewerBraces to be enabled.

1reaction
griggtcommented, Feb 15, 2022

The issue does not manifest if fewer braces support is enabled by starting the REPL with the -language:experimental.fewerBraces command line option rather than using the import during the REPL session. Which leads me to suspect that the compiler context is not being (fully) updated upon first seeing the import statement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

fewerBraces allows empty indentation region #13097 - GitHub
Expect it to require a block expr and not happily eta-expand. ... Strange behaviour in repl when using fewerBraces, first it does not...
Read more >
This is definitely a strange p... - Replit
Hrmm...that sounds like a distinct possibility. I had that problem with Sublime and found that Repl.it handled the same code that erred out...
Read more >
On braces and indentation-based syntax - Scala Contributors
My vote and prayer to the coding God is that indenting lords over noisy braces. Fix it and make it consistent everywhere, no...
Read more >
Scala 3: Even Fewer Braces - Medium
This makes it easier to compare the differences. First, here is an examplemethod that takes a function argument, which takes no arguments: def ......
Read more >
Optional Braces - Scala 3 - EPFL
First, some badly indented programs are flagged with warnings. ... Generally, the rule is that adding a pair of optional braces will not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found