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.

Scala Wart: For-comprehensions syntax restrictions

See original GitHub issue

Opening this issue, as suggested by Martin, to provide a place to discuss the individual warts brought up in the blog post Warts of the Scala Programming Language and the possibility of mitigating/fixing them in Dotty (and perhaps later in Scala 2.x). These are based on Scala 2.x behavior, which I understand Dotty follows closely, apologies in advance if it has already been fixed


As mentioned above, you cannot have defs, classes, or imperative statements in the generators of a for-comprehension:

scala> for{
     |   i <- Seq(1)
     |   println(i)
     |   j <- Seq(2)
<console>:4: error: '<-' expected but ';' found.
  j <- Seq(2)
^

This is a rather arbitrary restriction, and as far as I can tell doesn’t serve any purpose, and forces you to put random _ = prefixes on your statements to make things compile:

scala> for{
     |   i <- Seq(1)
     |   _ = println(i)
     |   j <- Seq(2)
     | } yield j
1
res0: Seq[Int] = List(2)

There really isn’t any reason that this shouldn’t work out-of-the-box, and convert say:

for{
  i <- Seq(1)
  def debug(s: Any) = println("Debug " + s)
  debug(i)
  j <- Seq(2)
  debug(j)
  k <- Seq(3)
} yield i + j + k

Into

Seq(1).flatMap{ i => 
  def debug(s: Any) = println("Debug " + s)
  debug(i)
  Seq(2).flatMap{ j =>
    debug(j)
    Seq(3).map{ k => 
      i + j + k
    }
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:12
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
ritschwummcommented, May 29, 2017

see #1982, too

0reactions
oderskycommented, Jan 24, 2018

I don’t see a lot of appetite to tackle this, since it would require major changes to the principle and implementation of parsing. So it would be a high effort project that also comes with high risk of failure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Warts of the Scala Programming Language
This also gives Scala all the constraints of the JVM: boxing everywhere ... of for-comprehensions; For-comprehensions syntax restrictions ...
Read more >
For Comprehensions | Tour of Scala
A comprehension evaluates the body e for each binding generated by the enumerators and returns a sequence of these values. Here's an example:...
Read more >
A few signposts for your Scala Journey by - Signify Technology
In a nutshell, non-strict evaluation means that the arguments passed to a function are not evaluated before we execute the function (as is...
Read more >
Scala Pattern Matching Warts and Improvements
pattern matching warts, which are clearly unjustified limitations and quirks ... Scala (unlike standard Haskell) has a convenient syntax for ...
Read more >
Preface - Scala with Cats
The main method provided by the syntax for Functor is map . It's difficult to demonstrate this with Options and Lists as they...
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