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
121
votes
7 answers

What is the rationale behind having companion objects in Scala?

Is there a case where a companion object (singleton) for a class is needed? Why would I want to create a class, say Foo and also create a companion object for it?
Rahul
  • 12,886
  • 13
  • 57
  • 62
119
votes
6 answers

Kotlin: Difference between object and companion object in a class

What is the difference between an object and a companion object in a class in kotlin? Example: class MyClass { object Holder { //something } companion object { //something } } I already read that companion object…
Poweranimal
  • 1,622
  • 3
  • 12
  • 16
79
votes
3 answers

How does Scala's apply() method magic work?

In Scala, if I define a method called apply in a class or a top-level object, that method will be called whenever I append a pair a parentheses to an instance of that class, and put the appropriate arguments for apply() in between them. For…
Jeff
  • 14,831
  • 15
  • 49
  • 59
47
votes
6 answers

Access application context in companion object in kotlin

How can we access application context inside companion object in Android kotlin? I have a companion object inside an abstract class and I want to access context to read Shared Preferences, but I'm not able to get the context. UPDATE: I'm working…
Hamza
  • 741
  • 1
  • 6
  • 12
38
votes
4 answers

Why do case class companion objects extend FunctionN?

When you create a case class, the compiler creates a corresponding companion object with a few of the case class goodies: an apply factory method matching the primary constructor, equals, hashCode, and copy. Somewhat oddly, this generated object…
retronym
  • 54,768
  • 12
  • 155
  • 168
35
votes
8 answers

How to talk about companion objects vs regular objects?

I am teaching Scala for the first time and my students are finding the deliberate "punning" involved in companion objects very confusing. Consider the following example: class Stack { ... methods such as push/pop } object Stack { ... factory…
Chris Okasaki
  • 4,875
  • 1
  • 20
  • 19
20
votes
4 answers

Android/Kotlin: Error: "Expecting a top level declaration > Task :app:buildInfoGeneratorDebug"

I try to write a class to manage a SQLite DB, but I have the error message "Expecting a top level declaration > Task :app:buildInfoGeneratorDebug". package com.xexxxwxxxxs.GMP import android.database.sqlite.SQLiteDatabase import…
ΩlostA
  • 2,501
  • 5
  • 27
  • 63
20
votes
3 answers

Access methods outside companion object - Kotlin

I'm pretty new to kotlin and I was wondering if it's possible, and if it's against best practice to access methods and variables that are outside a companion object, from within the companion object. For example class A { fun doStuff(): Boolean…
alessandro gaboardi
  • 889
  • 3
  • 11
  • 26
20
votes
2 answers

companion object to a private class: why isn't it valid?

i needed two instances that has access to each other privates. i naturaly thought of a companion object that grants access to a one and only instance of it's companion class. the class itself i made private, so users cannot just create instances…
gilad hoch
  • 2,846
  • 2
  • 33
  • 57
18
votes
4 answers

how to access companion object from object instance in kotlin?

Simple sample class with companion object class MyClass { companion object { val test = 25 } } The value test can be accessed from by MyClass.test, but how to access from an instance of MyClass? If I have val sample = MyClass(), how do I…
innov8
  • 2,093
  • 2
  • 24
  • 31
14
votes
2 answers

Kotlin - Is it possible to initialize companion object before the init block in a class?

Is it possible to initialize an companion object before the init block in a Kotlin class? If so, how? If not, is there a way to accomplish the same thing. I have the following scenario, class A(val iname: String) { init { foo.add(this) } …
Filip Allberg
  • 3,941
  • 3
  • 20
  • 37
14
votes
3 answers

Kotlin object vs companion-object vs package scoped methods

I have written this methods in Kotlin and analysed the bytecode: Situation 1 class A { object b { fun doSomething() {} } } Situation 2 class A { companion object b { fun doSomething() {} } } Situation 3 fun…
johnny_crq
  • 4,291
  • 5
  • 39
  • 64
13
votes
1 answer

Companion class requires import of Companion object methods and nested objects?

I am looking at Akka related typesafe activator code and the following construct intrigued me: Companion object: object MarkerActor { sealed trait MarkerMessage case object Stop extends MarkerMessage .. def objectMethod = print("hi from…
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
13
votes
1 answer

What's the difference between a class with a companion object and a class and object with the same name?

A Scala class's "companion object" can be viewed as a singleton object with the same fully qualified name as the class (i.e. same name, in same package). They are used to hold utility functions common to all instances of the class, as a replacement…
12
votes
1 answer

Curried case class constructor on companion

When defining a case class, the default companion object has nice curried method to get a curried version of the case class constructor: scala> case class Foo(a: String, b: Int) defined class Foo scala> Foo.curried res4: String => (Int => Foo) =…
romanb
  • 6,391
  • 2
  • 20
  • 19
1
2 3
16 17