Scala companion class warning

I'm new to Scala programming, can someone explain the warnings below to me?

Scala-companion-warning I tried to find the reason, and this seems like a big fixed earlier: https://issues.scala-lang.org/browse/SI-6439

So why am I getting this warning?

+5
source share
2 answers

This applies to REPL (Read Evaluate Print Loop), because it cannot know when user input ends.

Use :paste to get around it:

 scala> class A {} defined class A scala> object A {} defined object A warning: previously defined class A is not a companion to object A. Companions must be defined together; you may wish to use :paste mode for this. scala> :paste // Entering paste mode (ctrl-D to finish) class A {} object A {} // Exiting paste mode, now interpreting. defined class A defined object A 
+10
source

The Companion class and its object must be defined in one file.

so that you can write your class and object in a file, and then with :paste in REPL you can run your code. warning will not appear.

 scala> :paste // Entering paste mode (ctrl-D to finish) class Student(sid: Int, sname: String) { val id = sid val name = sname override def toString() = this.id + "," + this.name } object Student { def displayDetails(st: Student) = { println("Student Details : " + st.id + "," + st.name) } } // Exiting paste mode, now interpreting. defined class Student defined object Student 
+2
source

All Articles