Scala companion class warning

后端 未结 2 576
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 04:07

I am new to Scala programming, can someone explain me below warning reason?

\"Scala-Companion-Warning\" I trie

2条回答
  •  鱼传尺愫
    2021-01-12 04:23

    Companion class and its object must be defined in the same file.

    so you can write your class and object in a file and then using :paste in REPL you can run your code. no warning will 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
    

提交回复
热议问题