hi, all Scala can infer the actual type if we didn’t declare its type. There also nothing different in the byte code of java class format. It’s convenient for write the code, but hard to read. Maybe it’s time to face such bad smell code. Scala check style plugin also have such rule. We can add this to the code guidelines.
So I have a proposal: Declare all the class field with its type as far as possible in Scala code. The variate in the function can be ignore if necessary. What do you think about this? The test below to show that declare type and type inference are the same after compiling. Thanks Jinkui Shi -------------------------------------------------------------------------------------------------- object DeclareTypeTest { def main(args: Array[String]): Unit = { // 1 declareTypeFunction // 2 undeclareTypeFunction } def declareTypeFunction: Int = { 1 } def undeclareTypeFunction: Int = { 1 } } java -c DeclareTypeTest$.class : public void main(java.lang.String[]); Code: 0: aload_0 1: invokevirtual #18 // Method declareTypeFunction:()I 4: pop 5: aload_0 6: invokevirtual #21 // Method undeclareTypeFunction:()I 9: pop 10: return public int declareTypeFunction(); Code: 0: iconst_1 1: ireturn public int undeclareTypeFunction(); Code: 0: iconst_1 1: ireturn