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

Copying elemets from another list (saved in companion object) and modfying them also reflecting modifications to the original companion object

I have an Activity A which has: class ActivityA { companion object { var list: MutableList = //objects are acquired here. } } In ActivityB, I copy this list to a variable. class ActivityB { var copyList:…
Azizjon Kholmatov
  • 1,136
  • 1
  • 13
  • 26
0
votes
1 answer

How to copy companion object and make changes without reflecting these changes to the original object?

I am using companion object to temprorarily save some data. I might want to change this data and also i want to make sure that original object is not changed when i make changes. I tried this. companion object{ var position: Int = 0 }…
Azizjon Kholmatov
  • 1,136
  • 1
  • 13
  • 26
0
votes
2 answers

How to use Companion object in the main class

in the below 2 posted examples, i am trying to convert the java code to kotlin code. the kotlin code does not work and IntelliJ says that I should use companion object. please let me know how to correct this error. code_kotlin* …
Amrmsmb
  • 1
  • 27
  • 104
  • 226
0
votes
3 answers

Can I pass objects through Companion object?

I have a question about companion object in kotlin for Android: Instead of using extras or bundles, can I pass objects through companion object? I've already tried this and it's work, but I don't know if this is a good method or should it be…
Ady
  • 230
  • 3
  • 16
0
votes
2 answers

How can I specify that a class passed in a type parameter has a certain companion object?

I have these two traits: sealed trait DbValue { type R type T <: DbValue def content(): R def copy(newContent: R = content): Option[T] def toString(): String } sealed trait DbValueOps { type R type T <: DbValue def…
Frederik Baetens
  • 781
  • 1
  • 9
  • 20
0
votes
1 answer

When is a string built / created / compiled?

When will these string get their value and should they all produce the same value? class StringFactory{ companion object{ val str1 = App.shared.userSettings.getString(key, "") val str2: String get(){ return…
Joel Broström
  • 3,530
  • 1
  • 34
  • 61
0
votes
0 answers

Does enum retain static values and if so how do I create a new instance/reset it?

I wrote a string validator to check username/password. I pass a string, a min required length. The length of the first string i pass is 4 (username) and the second time 8 (password). I then store these values in a companion object and let my return…
Joel Broström
  • 3,530
  • 1
  • 34
  • 61
0
votes
1 answer

Scala - duplicated hierarchy for companion object

I'm pretty new to Scala, and I was wondering which is the best way to organize the hierarchy for classes and their companion objects. Suppose that I have a base class or interface that I want to extend. In Python I'd do something like this: class…
se7entyse7en
  • 4,310
  • 7
  • 33
  • 50
0
votes
2 answers

Scala change parameters of function defined in trait

Because I didn't find a solution to my problem anywhere, I think I might be thinking into a very wrong direction. Here's my problem: I have a trait A and another trait B and companion objects AB1, AB2, AB3 and so on. The singleton objects extend…
0
votes
0 answers

Could not find implicit value for parameter defined in companion object

In the minimal type class example below, scala 2.12.6 / sbt 1.2.1 complains could not find implicit value for parameter tc: tryout.Tryout.TypeClassTrait[Int]. If I uncomment the println line, it compiles. I was expecting this to work without the…
Georg
  • 966
  • 8
  • 25
0
votes
1 answer

How to access a Scala object instance given it's full qualified name?

So I have defined a Scala object using the following code: trait SomeTrait { def someMethod(): Unit } package somepackage1 { object Impl1 extends SomeTrait { def someMethod(): Unit = { } } object Impl2 extends SomeTrait { def…
vatsal mevada
  • 5,148
  • 7
  • 39
  • 68
0
votes
1 answer

How to reference a Scala inner class from the companion object

I want to have code like this: package test object Outer { import Outer._ implicit class OuterInt(val self: Int) extends AnyVal { def *(that: test.Outer.Inner) = that * self def +(that: Outer.Inner) = that + self …
Troy Daniels
  • 3,270
  • 2
  • 25
  • 57
0
votes
2 answers

In scala, how I can decide between class + companion object vs just the object

I am not sure if I should choose a class with a companion object in scala instead of using just an object. I just want to use the object anyway, but just because I only need one instance of the class. On the other hand, I am reading that classes…
0
votes
1 answer

Databinding TypeConverter error in kotlin

I'm using TypeConverter in some of my data-bindings. The issue is that it requires static functions and when I convert it into Kotlin it goes into the companion object and data-binding processor can't track the change. I get the following…
ir2pid
  • 5,604
  • 12
  • 63
  • 107
0
votes
0 answers

Accessing class parameters from companion object in Scala

While declaring a class in Scala, we can pass class parameters which are then used by compiler to generate a primary constructor: class Rational(n:Int,d:Int){ override def toString = n + "/"+ d } //companion object object Rational{ def…
Mandroid
  • 6,200
  • 12
  • 64
  • 134