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
6
votes
1 answer

Kotlin: class inherits of an interface that contains companion object, but doesn't have access to the constants of the interface

Here's the interface: interface SomeInterface { companion object { const val MY_CONST = "the constant" } } And then, the class (without body because is only an example): class SomeClass : SomeInterface After this, when I try to call…
Mário Henrique
  • 101
  • 2
  • 6
6
votes
2 answers

kotlin abstract static fun in companion objects

I learn the use of ViewHolder from an offical sample named UserViewHolder. public class UserViewHolder extends RecyclerView.ViewHolder { static UserViewHolder create(LayoutInflater inflater, ViewGroup parent) { UserItemBinding binding =…
jatwing
  • 63
  • 1
  • 1
  • 7
6
votes
2 answers

Can't get an access to a companion object's field

I'm wondering, why can't a class get an access to a companion object's field? class MyClass { println(val1) // not found, why? } object MyClass { val val1 = "str" } It should, it should even have an access to the private fields of object…
Incerteza
  • 32,326
  • 47
  • 154
  • 261
5
votes
1 answer

automatically generate case object for case class

How can I have the scala compiler automatically generate the case object? // Pizza class class Pizza (val crust_type: String) // companion object object Pizza { val crustType = "crust_type" } Desired properties for case object for each…
Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
5
votes
2 answers

Scala Case Class Companion Objects - Conflict on the type name

I am facing an issue with Companion Objects picking up its type instead of the case class I am using spray json serdes. They need an implicit JsonFormat. This format is obtained by calling a function that depends on the number of parameters of the…
xmar
  • 1,729
  • 20
  • 48
5
votes
1 answer

how to access outer class' javaClass.simpleName from companion object in kotlin?

I would like to be able to access the simpleName of my class from it's companion object. I would like this: val o1 = Outer("foo") val o2 = Outer("bar") to print the following output: Outer: hello Outer: foo Outer: bar The actual use case would be…
Gavriel
  • 18,880
  • 12
  • 68
  • 105
5
votes
1 answer

How to deprecate the companion object of a case class?

I noticed that if a case class is deprecated its companion object is not. scala> @deprecated case class A(x: Int) warning: there was one deprecation warning; re-run with -deprecation for details defined class A scala> A(0) res0: A = A(0) scala>…
Michael
  • 41,026
  • 70
  • 193
  • 341
5
votes
1 answer

Why do we have to explicitly import implicit conversions having implicit parameters from companion objects? Strange.

Let's consider this code: class A object A{ implicit def A2Int(implicit a:A)=1 implicit def A2String(a:A)="Hello" } object Run extends App{ implicit val a: A =new A import A.A2Int // without this import this code does not compile, why ? …
jhegedus
  • 20,244
  • 16
  • 99
  • 167
5
votes
1 answer

Scala: Get companion class in companion object

I need to define a val in my companion object which is initialized with a method which takes the companion class as parameter. I want to handle this with traits to not repeat myself. My Problem ist, that X.getClass ist not the same as classOf[X].…
user1563700
5
votes
2 answers

Overriding Companion Object Values and Scala MatchError

Can someone clarify why the following code causes a MatchError? What does a MatchError mean in this case? class A { def g = A.f } object A { val f = "Object A" } class B extends A { override val A.f = "Object B" } val b = new B b.g Given…
deepkimo
  • 3,187
  • 3
  • 23
  • 21
5
votes
3 answers

scala: prevent companion Object from hiding definition of the original Class

I think this is easier to show with an example. Let's say I have a Condition case class, with a Condition companion object used to provide an alternative constructor, like this: case class Condition( field: String, values:…
opensas
  • 60,462
  • 79
  • 252
  • 386
4
votes
2 answers

Kotlin : function extension inside companion object

In Kotlin language, what does this syntax do and how does it work ? class ClassName1 { companion object { fun ClassName2.funName()="" } }
Imane
  • 75
  • 1
  • 4
4
votes
1 answer

Property would not be serialized into a 'Parcel'. Add '@IgnoredOnParcel' annotation to remove the warning

Hey I am working in android Kotlin. I am getting this weird warning Property would not be serialized into a 'Parcel'. Add '@IgnoredOnParcel' annotation to remove the warning I don't want to add @IgnoredOnParcel this annotation in my class. I want…
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
4
votes
0 answers

Scala how to create implicit object with same name as function

Given the following trait and companion object definitions: trait Api { def foo(s: String): String } object Api extends Api { override def foo(s: String) = s } I would like to extend the companion object's foo method to have a property…
smac89
  • 39,374
  • 15
  • 132
  • 179
4
votes
1 answer

Kotlin - What does companion object fun do?

Declaring a "static" function in Kotlin is done using: companion object { fun classFoo() { //do something } } However I was mistakenly coding companion object fun classFoo() { //do something } Expecting the code to do the…
htafoya
  • 18,261
  • 11
  • 80
  • 104
1 2
3
16 17