Struggle against habits formed by Java when migrating to Scala

后端 未结 7 772
星月不相逢
星月不相逢 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:30

    Using Arrays.

    This is basic stuff and easily spotted and fixed, but will slow you down initially when it bites your ass.

    Scala has an Array object, while in Java this is a built in artifact. This means that initialising and accessing elements of the array in Scala are actually method calls:

    //Java
    //Initialise
    String [] javaArr = {"a", "b"};
    //Access
    String blah1 = javaArr[1];  //blah1 contains "b"
    
    //Scala
    //Initialise
    val scalaArr = Array("c", "d")  //Note this is a method call against the Array Singleton
    //Access
    val blah2 = scalaArr(1)  //blah2 contains "d"
    

提交回复
热议问题