Struggle against habits formed by Java when migrating to Scala

后端 未结 7 697
星月不相逢
星月不相逢 2021-02-01 08:48

What are the most common mistakes that Java developers make when migrating to Scala?

By mistakes I mean writing a code that does not conform to Scala spirit, for example

7条回答
  •  走了就别回头了
    2021-02-01 09:36

    I haven't adopted lazy vals and streams so far.

    In the beginning, a common error (which the compiler finds) is to forget the semicolon in a for:

     for (a <- al;
          b <- bl
          if (a < b)) // ...
    

    and where to place the yield:

     for (a <- al) yield {
         val x = foo (a).map (b).filter (c)
         if (x.cond ()) 9 else 14 
     }
    

    instead of

     for (a <- al) {
         val x = foo (a).map (b).filter (c)
         if (x.cond ()) yield 9 else yield 14  // why don't ya yield!
     }
    

    and forgetting the equals sign for a method:

     def yoyo (aka : Aka) : Zirp { // ups!
         aka.floskel ("foo")
     }
    

提交回复
热议问题