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

Avoid duplicate type alias in Scala class and companion object

I'm a newbie in Scala, and I have a Scala program with a class and a companion object, and I want to use a type alias that is used : To define the methods of the class. Also I want to use the alias outside the class. For that I find useful to…
juanrh0011
  • 323
  • 2
  • 14
0
votes
0 answers

Companion Objects of Scala's Traits

I have something like this: trait AO extends A { private var link: String = "AO" //... } trait AR extends A { private var link: String = "AR" //... } object AO extends AO with Atr { atr += link } object AR extends AR with Atr { atr…
Valerin
  • 465
  • 3
  • 19
0
votes
2 answers

How can I guarantee the existence of a method in a companion object and reference it?

Consider this example, where Listable is intended to mixed into the companion object of a case class. Therefore, in order to call Writer.grid, one must have a companion object A that extends Listable[A], with an implicit Writer[A] defined within.…
Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
0
votes
1 answer

Access object inside of a companion object in scala

I have the following: case class Location(name: String, level: Location.Level) object Location { trait Level case object City extends Level case object State extends Level } If I try and access City (from another source file), I get an error…
three-cups
  • 4,375
  • 3
  • 31
  • 41
0
votes
1 answer

Create companion objects for classes imported from Java

I want to create companion objects for some imported Java types, so that I do not have to use new to allocate them. I want to start with the type Vector3f is imported from com.jme3.math from jMonkeyEngine. What I tried is: package…
Suma
  • 33,181
  • 16
  • 123
  • 191
0
votes
1 answer

When is an implicit imported from a companion object?

I know this is the umpteenth implicits / companion object question. However, I did not find this case anywhere yet. In "Tryout", why does A need no import for the implicit class, while B does need it? Thankx. class LongConsumer { def consume(l:…
Georg
  • 966
  • 8
  • 25
0
votes
2 answers

Scala equivalent of static final using a companion object

How could I convert the following into scala. public class JedisDB { private static final JedisPool jedisPool = new JedisPool(getJedisPoolConfig()); public static JedisPoolConfig getJedisPool() { // .. } public int getTest123() { …
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
0
votes
1 answer

Override toString method is not getting called from Companion Object

I thought I can call methods of class once I created the object of that class through companion object. But I am not able to do that. Below is my code: class Employee(val id: Int, val initialBalance: Int) { val message = println("Object created…
Sudipta Deb
  • 1,040
  • 3
  • 23
  • 43
0
votes
2 answers

Encapsulating related fields into a companion object/class and avoiding duplication

My application uses a properties-file to load several properties. Every instance of the application has 3 environment-related parameters - one of them is a property, the other two are computed based on it. class Environment(val properties:…
teo
  • 1,393
  • 1
  • 15
  • 25
0
votes
1 answer

Companion Object in a List bound by the Related class' super-trait

I am trying put together a "registry" of Companion Objects - by storing them in a List which is bound using Generics. An example is best: trait Foo case class A() extends Foo object A case class B() extends Foo object B case class C() extends…
Ramon
  • 401
  • 6
  • 8
0
votes
2 answers

How to replace static variables with scala available means in a base class

What I need: trait Base { import scala.math.{Pi=>mPi, cos=>msoc, sin=>msin} static val Pi : Float = mPi.toFloat static def cos(phi : Float) : Float = mcos(phi).toFloat static def sin(phi : Float) : Float = msin(phi).toFloat // ... //…
ayvango
  • 5,867
  • 3
  • 34
  • 73
0
votes
2 answers

method name qualification when using a companion object

I am just learning Scala. I created a companion object (see code snippet below) where I define an operator, ^, (to represent complex conjugation). I have to qualify it with the companion objects name inside the associated class. I was under the…
-1
votes
1 answer

Why fields declaration with var/val not allowed in apply method of companion object

I am using scala 2.12 and IntelliJ IDE. I tried to auto-generate apply method in Scala companion object using readily available template. It generated apply method with fields having val because I have created class having fields declared as val in…
Gunjan Shah
  • 5,088
  • 16
  • 53
  • 72
-1
votes
1 answer

How to mix in self annotations in companion object

Consider the following situation: trait M { } // uses N object M { def apply = new M { } } trait N { self: L => } trait L { val m: B } type B M needs new trees definitions that are created in N. I don't want to pass around these new trees…
user1868607
  • 2,558
  • 1
  • 17
  • 38
-1
votes
2 answers

Private value of a Scala class is not accessible in the companion Object

I have a Scala class and it's companion object (in Client.scala) as follows: class Client(val key: Int) { private val num_bits = 5 } object Client { val count_entries = Math.pow(2, num_bits).toInt println(count_entries) } However,…
PankajK
  • 187
  • 3
  • 14
1 2 3
16
17