Questions tagged [companion-object]

In Scala and Kotlin, an object with the same name as a class, used to hold utility members for the class

In and , a companion object is an object with the same fully qualified name as a class. Companion objects are used to hold utility members that apply to all instances of the class. These can include constants, s (apply), and s. They provide a similar functionality as Java's .

241 questions
12
votes
3 answers

Why is a Scala companion object compiled into two classes(both Java and .NET compilers)?

object ScalaTrueRing { def rule = println("To rule them all") } this piece of code will be compiled into java byte code, if I decompile it, then the equivalent Java code is similar to this: public final class JavaTrueRing { public static final…
11
votes
2 answers

In Scala, how can I define a companion object for a class defined in Java?

I'd like to add implicit conversions to Java classes generated by a modeling tool. So I want to add them to the companion object of those classes, so that the compiler automatically finds them. But I cannot add them in a separate file, because the…
11
votes
2 answers

When to use companion object factory versus the new keyword

Many classes in the Scala standard library use apply() of their companion object as factory. This is often convenient when chaining calls like List(List(1)). On the other hand, it's still possible to create objects directly with new (new…
Mifeet
  • 12,949
  • 5
  • 60
  • 108
10
votes
1 answer

Companion objects - does Android want to register them as activities (?)

I have an Activity class DummyActivity : Activity() { companion object { @JvmStatic fun onNewIntent(context: Context): Intent { val intent = Intent(context, DummyActivity.javaClass) return intent } …
AndrzejZ
  • 245
  • 2
  • 10
10
votes
3 answers

How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?

I have a case class defined as such: case class StreetSecondary(designator: String, value: Option[String]) I then define an explicit companion object: object StreetSecondary { //empty for now } The act of defining the explicit companion…
chaotic3quilibrium
  • 5,661
  • 8
  • 53
  • 86
8
votes
2 answers

Companion object cannot access private variable on the class

A rather weird behavior coming from the Scala REPL. Although the following compiles without a problem: class CompanionObjectTest { private val x = 3 } object CompanionObjectTest { def testMethod(y:CompanionObjectTest) = y.x + 3 } the…
dimitrisli
  • 20,895
  • 12
  • 59
  • 63
8
votes
3 answers

How to implement a counter in functional programming way

While trying to understand companion objects, I have written following code which counts the number of times a class was instantiated. I had to use a 'var' to keep the count. Is there a 'functional programming' way to achieve the same task i.e. use…
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
8
votes
1 answer

Can't access Parent's Members while dealing with Macro Annotations

I am kind of blocked with the following (macro annotation) situation. Suppose I have an annotation called @factory which aims to generate an apply method for the annotated trait in the corresponding companion object. For instance, given the trait…
neutropolis
  • 1,884
  • 15
  • 34
7
votes
2 answers

classifier does not have a companion object

i want to use BottomNavigationView in my app and i'm facing this problem with kotlin (never had it before with java) i see this message : classifier 'listFragment' does not have a companion object and thus must be initialized here this is my code…
evals
  • 1,750
  • 2
  • 18
  • 28
7
votes
3 answers

How can I make reference to companion objects from Java?

I have a mixed project, Java and Kotlin classes, and I want to know how I can refer to companion objects from my Java classes.
user7997694
7
votes
1 answer

A class imported from a companion not usable as the constructor parameter default value

Consider following code: object Main extends App { object Project { case class Config(rules: Seq[String] = Seq.empty) } import Project._ //case class Project(root: String, config: Config) // compiles fine //case class Project(root:…
Suma
  • 33,181
  • 16
  • 123
  • 191
7
votes
2 answers

Scala type alias including companion object [beginner]

I'd like to write a type alias to shorten, nice and encapsulated Scala code. Suppose I got some collection which has the property of being a list of maps, the value of which are tuples. My type would write something like List[Map[Int, (String,…
Jean
  • 10,545
  • 2
  • 31
  • 31
7
votes
1 answer

Create or extend a companion object, using a macro annotation on the class

Using a Scala 2.10/2.11 macro paradise annotation macro, how can I add or extend the companion object of an annotated class? Skeleton: import scala.annotation.StaticAnnotation import scala.reflect.macros._ import language.experimental.macros class…
0__
  • 66,707
  • 21
  • 171
  • 266
7
votes
2 answers

Do final vals increase the object size?

class Foo { final val pi = 3 } Does every Foo object have a pi member? Should I therefore put pi in the companion object?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
7
votes
2 answers

Typing over companion object in Scala

I have a class and its companion object which together have some reusable functionality. I have encapsulated the functionality of the companion object into a trait, so now the situation is like class Foo { import Foo._ def foo: Quux =…
Andrea
  • 20,253
  • 23
  • 114
  • 183
1
2
3
16 17