companion-object

What's the difference between a class with a companion object and a class and object with the same name?

谁说我不能喝 提交于 2019-12-21 07:22:41
问题 A Scala class's "companion object" can be viewed as a singleton object with the same fully qualified name as the class (i.e. same name, in same package). They are used to hold utility functions common to all instances of the class, as a replacement for Java's static methods. However, in various places in the docs and in questions, it say that companion objects must be defined in the same compilation unit. For example, they must be defined in the same file; companion objects cannot be defined

Why can't Scala find my typeclass instance defined implicitly in the companion object, when the typeclass is not in a dedicated source file?

不打扰是莪最后的温柔 提交于 2019-12-17 21:12:51
问题 Please refer to the source code below. All source code is defined in the same package. When I define all of the code within a single source file ShowMain.scala , I get a compile error, however when object ShowMain is defined in ShowMain.scala and trait Show and object Show are defined in Show.scala , there is no compile error. My question: What is the cause for this? What language rule have I run afoul of? Example code: object ShowMain { def main(args: Array[String]): Unit = { output("hello")

automatically generate case object for case class

感情迁移 提交于 2019-12-13 03:16:39
问题 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 attribute in the case class generate an attribute in the case object set the value in of each corresponding case object to the string representation of the attribute name and change camelCase to snake_case for the object attribute name, keep snake_case for

method name qualification when using a companion object

给你一囗甜甜゛ 提交于 2019-12-11 18:36:38
问题 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 impression that I should have unqualified access to the methods of the companion. Can someone tell me if I have done something wrong? class CQ(val r: Q, val i:Q) { def +(z : CQ): CQ = { return new CQ(r + z.r, i + z.i) } def -(z: CQ): CQ = { return new

Order of definition matters in Scala?

旧时模样 提交于 2019-12-11 11:42:44
问题 When looking for implicits, the Scala compiler looks, among other places, in the companion object of the various parts of the classes involved. Apparently, though, it fails to perform this lookup when the implicit conversion is used in the class itself, if it is defined before the companion object. The minimal example I was able to cook up is: trait Counter[A] { def count(a: A): Int } object Foo { def foo[A](a: A)(implicit c: Counter[A]) = c.count(a) } case class Bar(id: Int) { import Foo._

Scala implicit typeclass precedence in companion objects

廉价感情. 提交于 2019-12-11 01:05:08
问题 trait Eq[-A] { def eq(a: A, b: A): Boolean } object Eq { implicit object IntEq extends Eq[Int] { def eq(a: Int, b: Int) = a == b } } trait Supertrait[+A] object Supertrait { implicit def Eq[A: Eq]: Eq[Supertrait[A]] = ??? } trait Subtrait[+A] extends Supertrait[A] object Subtrait { implicit def Eq[A: Eq]: Eq[Subtrait[A]] = ??? } def f[A](x: Subtrait[A])(implicit ev: Eq[Subtrait[A]]) = ??? f(new Subtrait[Int] {}) When compiling this code, the following error occurs: Error:(32, 4) ambiguous

Typing over companion object in Scala

ぃ、小莉子 提交于 2019-12-10 04:12:08
问题 I have a class and its companion object which together have some reusable functionality. I have encapsulated the functionality of the companion object into a trait, so now the situation is like class Foo { import Foo._ def foo: Quux = bar(this) } trait Bar { def bar(f: Foo): Quux = frobnicate(f) } object Foo extends Bar Since Foo.foo is a reusable method, I would like to put it into its trait. But I have to find a way to tell the type checker that, although bar is not a method on class Foo ,

Scala type alias including companion object [beginner]

左心房为你撑大大i 提交于 2019-12-10 03:39:28
问题 I'd like to write a type alias to shorten, nice and encapsulated Scala code. Suppose I got some collection which has the property of being a list of maps, the value of which are tuples. My type would write something like List[Map[Int, (String, String)]] , or anything more generic as my application allows it. I could imagine having a supertype asking for a Seq[MapLike[Int, Any]] or whatever floats my boat, with concrete subclasses being more specific. I'd then want to write an alias for this

access to a type's companion object

不羁的心 提交于 2019-12-06 09:40:01
I have a variable of some type and I'd like to get information from the companion object. For example, I thought I might be able to do something like this: def foo[I: Integral](i:I): = { val minVal = i match { case _:Byte => Byte.MinValue case _:Char => Char.MinValue case _:Int => Int.MinValue case _:Long => Long.MinValue case _:Short => Short.MinValue } // compare i and minVal } But this is rather verbose and minVal comes out as :Long which complicates comparisons with i: I . I was hoping I could find something concise and direct but I suspect this requires reflection, which is often neither.

Typing over companion object in Scala

核能气质少年 提交于 2019-12-05 05:47:58
I have a class and its companion object which together have some reusable functionality. I have encapsulated the functionality of the companion object into a trait, so now the situation is like class Foo { import Foo._ def foo: Quux = bar(this) } trait Bar { def bar(f: Foo): Quux = frobnicate(f) } object Foo extends Bar Since Foo.foo is a reusable method, I would like to put it into its trait. But I have to find a way to tell the type checker that, although bar is not a method on class Foo , it will be in scope because imported from the companion object. I think I need something like being