companion-object

Kotlin: Difference between object and companion object in a class

六眼飞鱼酱① 提交于 2019-11-30 05:34:04
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 shall be used, if the containing parameters/methods are closely related to its class. But why is there also the possibility of declaring a normal object in the class? Because it behaves exactly like the companion, but it must have a name. Is there maybe a difference in its "static" (I'm from the java side) lifecycle? Objects can implement interfaces. Inside a class, defining a simple

Companion object cannot access private variable on the class

…衆ロ難τιáo~ 提交于 2019-11-29 14:39:23
问题 A rather weird behavior coming from the Scala REPL. Although the following compiles without a problem: class CompanionObjectTest { private val x = 3 } object CompanionObjectTest { def testMethod(y:CompanionObjectTest) = y.x + 3 } the private variable does not seem to be accessible from the companion object in REPL: scala> class CompanionObjectTest { | | private val x = 3; | } defined class CompanionObjectTest scala> object CompanionObjectTest { | | def testMethod(y:CompanionObjectTest) = y.x

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

こ雲淡風輕ζ 提交于 2019-11-29 03:53:42
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 companion object") } Companion class: it imports the companion object methods: class MarkerActor extends Actor with ActorLogging { import MarkerActor._ // Comment this line to compare w or w/o import available objectMethod // just to see if 'visible' within companion class override def receive = { case Stop => { So.. that is a bit surprising. Why is there not a

How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?

て烟熏妆下的殇ゞ 提交于 2019-11-28 09:24:17
I have a case class defined as such: case class StreetSecondary(designator: String, value: Option[String]) I then define an explicit companion object: object StreetSecondary { //empty for now } The act of defining the explicit companion object StreetSecondary causes the compiler produced "implicit companion object" to be lost; i.e. replaced with no ability to access the compiler produced version. For example, the tupled method is available on case class StreetSecondary via this implicit companion object. However, once I define the explicit companion object, the tupled method is "lost". So,

In Scala, how can I define a companion object for a class defined in Java?

廉价感情. 提交于 2019-11-28 00:58:29
I'd like to add implicit conversions to Java classes generated by a modeling tool. So I want to add them to the companion object of those classes, so that the compiler automatically finds them. But I cannot add them in a separate file, because the companion has to be defined in the same file. Is there anything I can do about this? Of course, I can define all my implicit conversions in another object and then bring it into scope, but this requires an extra import. Any other solution? With the Scala compiler as it stands now there is no way to define companion objects other than by putting them

Why do case class companion objects extend FunctionN?

若如初见. 提交于 2019-11-27 18:58:40
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 extends FunctionN. scala> case class A(a: Int) defined class A scala> A: (Int => A) res0: (Int) => A = <function1> This is only the case if: There is no manually defined companion object There is exactly one parameter list There are no type arguments The case class isn't abstract. Seems like this was added about two years ago. The latest incarnation

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

ε祈祈猫儿з 提交于 2019-11-27 17:50:52
问题 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 companion object") } Companion class: it imports the companion object methods: class MarkerActor extends Actor with ActorLogging { import MarkerActor._ // Comment this line to compare w or w/o import available objectMethod // just to see if 'visible' within

Can't access Parent's Members while dealing with Macro Annotations

天涯浪子 提交于 2019-11-27 08:57:55
I am kind of blocked with the following ( macro annotation ) situation. Suppose I have an annotation called @factory which aims to generate an apply method for the annotated trait in the corresponding companion object. For instance, given the trait A : @factory trait A { val a1: Int } the expected code to be generated is the following one: object A extends Factory[A] { def apply(_a1: Int) = new A { val a1 = _a1 } } Now suppose we have a trait B which inherits from A : @factory trait B extends A { val b1: String } which is supposed to generate: object B extends Factory[B] { def apply(_a1: Int,

How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?

[亡魂溺海] 提交于 2019-11-27 01:59:22
问题 I have a case class defined as such: case class StreetSecondary(designator: String, value: Option[String]) I then define an explicit companion object: object StreetSecondary { //empty for now } The act of defining the explicit companion object StreetSecondary causes the compiler produced "implicit companion object" to be lost; i.e. replaced with no ability to access the compiler produced version. For example, the tupled method is available on case class StreetSecondary via this implicit

In Scala, how can I define a companion object for a class defined in Java?

我与影子孤独终老i 提交于 2019-11-26 23:29:14
问题 I'd like to add implicit conversions to Java classes generated by a modeling tool. So I want to add them to the companion object of those classes, so that the compiler automatically finds them. But I cannot add them in a separate file, because the companion has to be defined in the same file. Is there anything I can do about this? Of course, I can define all my implicit conversions in another object and then bring it into scope, but this requires an extra import. Any other solution? 回答1: With