Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala companion class warning

Tags:

scala

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

Scala-Companion-Warning I tried to find the reason and it seems like a big fixed earlier: https://issues.scala-lang.org/browse/SI-6439

So why I am getting this warning?

like image 439
Pankaj Avatar asked Apr 10 '15 05:04

Pankaj


People also ask

What is the advantage of companion objects in Scala?

Advantages of Companion Objects in Scala Companion objects provide a clear separation between static and non-static methods in a class because everything that is located inside a companion object is not a part of the class's runtime objects but is available from a static context and vice versa.

Can traits have companion objects?

A companion object is an object with the same name as a class or trait and defined in the same source file as the associated file or trait.

What is companion class in Scala?

A companion object in Scala is an object that's declared in the same file as a class , and has the same name as the class. For instance, when the following code is saved in a file named Pizza.scala, the Pizza object is considered to be a companion object to the Pizza class: class Pizza { } object Pizza { }

How do I create a static variable in Scala?

There are no static variables in Scala. Fields are variables that belong to an object. The fields are accessible from inside every method in the object. Fields can also be accessible outside the object, depending on what access modifiers the field is declared with.


1 Answers

This is specific to the REPL (Read Evaluate Print Loop), since it can't 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
like image 112
Marth Avatar answered Sep 22 '22 19:09

Marth