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

Unable to create companion class instance in companion object method

What's wrong with this code: class Trivials(s:String){ private val x = 0 } object Trivials { def main(args: Array[String]): Unit = { Trivials t = new Trivials("Trivials") } } Both class and object are defined in same source file, hence…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
0
votes
1 answer

Creating a DB factory using kotlin

So I'm trying to create a MongoDB factory with kotlin... but I think I don't really understand the concept of companion object very well because I can't even get this to compile: package org.jgmanzano.storage import com.mongodb.MongoClient import…
Javier García Manzano
  • 1,024
  • 2
  • 12
  • 25
0
votes
1 answer

Call superclass apply method in scala

trait A { def a def b def c } object A { def apply = { new A { def a = 1 def b = 2 def c = 3 } } } See i have a trait A here and the companion object's apply method implements…
0
votes
3 answers

Scala class singleton with non-default constructor parameters

I have already read about issues with objects inheriting from companion classes. For example: Class constructor parameter with default value causes companion object initializer to fail super constructor cannot be passed a self reference unless…
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
0
votes
1 answer

Implement companion object / factory in a separate submodule

In my core sbt module, I have an trait Matrix for a matrix data type, and a trait MatrixInstance that has some factory methods. The companion object is supposed to extend this trait. In a separate sbt submodule implementation1, I would like to…
Luciano
  • 2,388
  • 1
  • 22
  • 33
0
votes
1 answer

Scala: sealed trait objects are not working inside case statement: Akka actors

I have been struggling to deal with the below error related to sealed trait object usage in case statement.This is my code related to akka actors.I am using companion Objects and defined the sealed traits as shown in below scala…
Mahesh
  • 178
  • 3
  • 14
0
votes
1 answer

Scala A companion Object Vs. Scala Class new Object

Here is the code and the output: //A scala class with a private variable and one method class A { private var a1 = "Zoom" def A1(): Unit = { println("A1 Class") } } //A companion object object A { def A2(): Unit = { println("A1…
Pardeep Sharma
  • 572
  • 5
  • 20
0
votes
2 answers

Scala: which classes should not have companion objects?

The following pattern seems to be idiomatic scala: class Foo() {} object Foo { def apply() = new Foo() } val x = Foo() What is the motivation for the idiomatic approach? In which cases should I not provide the factory method, forcing client to use…
0
votes
1 answer

Companion objects hide class -- bug or feature?

In Kotlin, the following seems to be reasonable code: data class Foo(val bar: String) { fun combine(other: Foo): Foo { return Foo(bar + other.bar) } companion object Foo { fun someHelper() {} } } However, it does…
Raphael
  • 9,779
  • 5
  • 63
  • 94
0
votes
1 answer

Scala Implicit Conversion for companion object of extended class

I am trying to create a customRDD in Java. RDD converts RDD[(K,V)] to PairRDDFunctions[K,V] using Scala implicit function rddToPairRDDFunctions() defined in object RDD. I am trying to do the same with my CustomJavaRDD which extends CustomRDD which…
0
votes
1 answer

How to override a implicit function of companion object?

I am new to Scala and implicit conversions. I am working with an existing scala library over which I have no control. Lets consider a class R from the library. abstract class R { } object R { implicit def RFunctions[K, V](r: R[(K, V)])…
0
votes
1 answer

calling methods of Controller from scala companion objects

I have a controller in my project which has a socket method I want to call that method in companion object.But somehow i am not able to do that as i need to pass parameters also to companion object , which i can't . Here's my code sample: class…
Aniket Pandey
  • 59
  • 1
  • 9
0
votes
3 answers

Create a companion object that mixes in a trait that defines a method which returns an object of the object's companion class

Abstract problem: Create a trait that can be mixed into the companion object of a class, to give that object a method that returns an object of that class. Concrete problem: I'm trying to create a bunch of classes for use with RESTful service calls,…
John Arrowwood
  • 2,370
  • 2
  • 21
  • 32
0
votes
1 answer

apply method in companion class is not printing output to console

I created a class ApplyDemo with private construtor as class ApplyDemo private{ override def toString()="ApplyDemo" } I created companion object of the class as object ApplyDemo { def apply()={ Console.println("calling…
optional
  • 3,260
  • 5
  • 26
  • 47
0
votes
1 answer

Problems returning a value while using a trait and companion object in Scala (Eclipse IDE)

I've placed the following code into an object.Scala file within Eclipse, and simply want to know what the value for "x" is (it should be 3). The code won't compile if I place the value anywhere other than within the List object; at the same time,…