companion-object

Scala type alias including companion object [beginner]

≯℡__Kan透↙ 提交于 2019-12-05 04:30:49
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 long type. class ConcreteClass { type DataType = List[Map[Int, (String, String)]] ... } I would then

“Static” field in Scala companion object

旧巷老猫 提交于 2019-12-02 11:37:22
问题 Can I check the value of field in companion object without referring to class' actual object? I'd like to store a static counter, increase it everytime new object of that class is created and be able to check it's value without using object itself, is this possible? 回答1: Is this what you want? object Foo { private var counter = 0 private def increment = { counter += 1; counter } } class Foo { val i = Foo.increment println(i) } 回答2: import java.util.concurrent.atomic.AtomicInteger object Foo {

“Static” field in Scala companion object

五迷三道 提交于 2019-12-02 05:05:01
Can I check the value of field in companion object without referring to class' actual object? I'd like to store a static counter, increase it everytime new object of that class is created and be able to check it's value without using object itself, is this possible? Is this what you want? object Foo { private var counter = 0 private def increment = { counter += 1; counter } } class Foo { val i = Foo.increment println(i) } import java.util.concurrent.atomic.AtomicInteger object Foo { val counter = new AtomicInteger(0) } class Foo { val i = Foo.counter.incrementAndGet() println(i) } 来源: https:/

Which among importing companion object or extending trait is better

江枫思渺然 提交于 2019-12-02 02:48:46
问题 I have a JSON protocol written in spray trait MyJsonProtocol { //some logic } object MyJsonProtocol extends MyJsonProtocol { } Now which is better ?? Importing this companion object or extending the trait ? 回答1: If you are creating some JsonFormat instances for spray, then you can just create an object directly and import that. This means that you only have a single instance of your implicit vals and objects. object MyJsonProtocol extends DefaultJsonProtocol { implicit object MyTypeJsonFormat

Which among importing companion object or extending trait is better

筅森魡賤 提交于 2019-12-01 23:43:32
I have a JSON protocol written in spray trait MyJsonProtocol { //some logic } object MyJsonProtocol extends MyJsonProtocol { } Now which is better ?? Importing this companion object or extending the trait ? If you are creating some JsonFormat instances for spray, then you can just create an object directly and import that. This means that you only have a single instance of your implicit vals and objects. object MyJsonProtocol extends DefaultJsonProtocol { implicit object MyTypeJsonFormat extends RootJsonFormat[MyType] { def write(v: MyType): JsValue = ... def read(value: JsValue): MyType = ...

Why is a Scala companion object compiled into two classes(both Java and .NET compilers)?

别来无恙 提交于 2019-12-01 03:50:25
object ScalaTrueRing { def rule = println("To rule them all") } this piece of code will be compiled into java byte code, if I decompile it, then the equivalent Java code is similar to this: public final class JavaTrueRing { public static final void rule() { ScalaTrueRing..MODULE$.rule(); } } /* */ public final class JavaTrueRing$ /* */ implements ScalaObject /* */ { /* */ public static final MODULE$; /* */ /* */ static /* */ { /* */ new (); /* */ } /* */ /* */ public void rule() /* */ { /* 11 */ Predef..MODULE$.println("To rule them all"); /* */ } /* */ /* */ private JavaTrueRing$() /* */ { /*

Why is a Scala companion object compiled into two classes(both Java and .NET compilers)?

▼魔方 西西 提交于 2019-12-01 00:48:41
问题 object ScalaTrueRing { def rule = println("To rule them all") } this piece of code will be compiled into java byte code, if I decompile it, then the equivalent Java code is similar to this: public final class JavaTrueRing { public static final void rule() { ScalaTrueRing..MODULE$.rule(); } } /* */ public final class JavaTrueRing$ /* */ implements ScalaObject /* */ { /* */ public static final MODULE$; /* */ /* */ static /* */ { /* */ new (); /* */ } /* */ /* */ public void rule() /* */ { /* 11

Create or extend a companion object, using a macro annotation on the class

谁都会走 提交于 2019-11-30 13:29:55
问题 Using a Scala 2.10/2.11 macro paradise annotation macro, how can I add or extend the companion object of an annotated class? Skeleton: import scala.annotation.StaticAnnotation import scala.reflect.macros._ import language.experimental.macros class foo extends StaticAnnotation { def macroTransform(annottees: Any*) = macro fooMacro.impl } object fooMacro { def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = ??? } Such that, given trait Foo[A] the following input @foo class Bar object

Kotlin: Difference between object and companion object in a class

大兔子大兔子 提交于 2019-11-30 13:06:17
问题 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"

Create or extend a companion object, using a macro annotation on the class

落花浮王杯 提交于 2019-11-30 07:22:03
Using a Scala 2.10/2.11 macro paradise annotation macro, how can I add or extend the companion object of an annotated class? Skeleton: import scala.annotation.StaticAnnotation import scala.reflect.macros._ import language.experimental.macros class foo extends StaticAnnotation { def macroTransform(annottees: Any*) = macro fooMacro.impl } object fooMacro { def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = ??? } Such that, given trait Foo[A] the following input @foo class Bar object Baz { def baz = 33 } @foo class Baz will be expanded as: object Bar { implicit def hasFoo: Foo[Bar] = ??