companion-object

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

馋奶兔 提交于 2019-11-26 22:18:01
问题 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

Why do case class companion objects extend FunctionN?

左心房为你撑大大i 提交于 2019-11-26 19:45:36
问题 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

How does Scala's apply() method magic work?

ぃ、小莉子 提交于 2019-11-26 19:42:42
In Scala, if I define a method called apply in a class or a top-level object, that method will be called whenever I append a pair a parentheses to an instance of that class, and put the appropriate arguments for apply() in between them. For example: class Foo(x: Int) { def apply(y: Int) = { x*x + y*y } } val f = new Foo(3) f(4) // returns 25 So basically, object(args) is just syntactic sugar for object.apply(args) . How does Scala do this conversion? Is there a globally defined implicit conversion going on here, similar to the implicit type conversions in the Predef object (but different in

What is the rationale behind having companion objects in Scala?

不羁岁月 提交于 2019-11-26 12:50:41
Is there a case where a companion object (singleton) for a class is needed? Why would I want to create a class, say Foo and also create a companion object for it? Saem The companion object basically provides a place where one can put "static-like" methods. Furthermore, a companion object, or companion module, has full access to the class members, including private ones. Companion objects are great for encapsulating things like factory methods. Instead of having to have, for example, Foo and FooFactory everywhere, you can have a class with a companion object take on the factory responsibilities

How does Scala&#39;s apply() method magic work?

北战南征 提交于 2019-11-26 07:25:18
问题 In Scala, if I define a method called apply in a class or a top-level object, that method will be called whenever I append a pair a parentheses to an instance of that class, and put the appropriate arguments for apply() in between them. For example: class Foo(x: Int) { def apply(y: Int) = { x*x + y*y } } val f = new Foo(3) f(4) // returns 25 So basically, object(args) is just syntactic sugar for object.apply(args) . How does Scala do this conversion? Is there a globally defined implicit

What is the rationale behind having companion objects in Scala?

浪尽此生 提交于 2019-11-26 03:06:35
问题 Is there a case where a companion object (singleton) for a class is needed? Why would I want to create a class, say Foo and also create a companion object for it? 回答1: The companion object basically provides a place where one can put "static-like" methods. Furthermore, a companion object, or companion module, has full access to the class members, including private ones. Companion objects are great for encapsulating things like factory methods. Instead of having to have, for example, Foo and