Ergonomics of for-comp synax under an IDE

Recently I was watching Automatic dependency injection in pure scala 3, and during the video, the speaker has to make a pause to basically describe the quirks of typing for comprehension in a IDE, where you’d typically type for { and the IDE will auto complete the other bracket, and this is invalid scala syntax. The author then goes on to explain that he adds a yield () to cope for a bit so that he can start writing the generators section.

I’ve been doing scala for close to 15 years now and this is definitely an issue I experience daily.
Every new members to projects who are new to scala, I have to explain to them this quirk so that the IDE doesn’t yell at them with incomprehensible errors product of the syntax not being complete.

The way the compiler interprets the syntax, essentially forces you to write in a spirally way, where you start in say line 1, move to 3, so that you can finally work on line 2, where you actually start writing the generators which is the part you care about.

I don’t have a solution to this, but this has been annoying me, my students, and everyone that watches a live scala coding session for ages now. Hopefully there’s something that could be done about it.

3 Likes

in metals and scala 3 I can start typing for , next line, then a generator and there is no error, and I can hover on x

for 
  x <- List(1,2)
1 Like

Was this all along my memory of times old (since I always add the yield clause before anything)? I apologize for the noise if so.
I just tested in scala 2 and 3 and indeed metals doesn’t break. It does give me an error highlight in essentially the next line saying something like “unexpected closing brace here” or “expected yield or blah blah”, but generally it seems to work.
I need to test more thoroughly with nested expressions and type inference.

Thanks!

1 Like