case-class

Why are case objects serializable and case classes not?

[亡魂溺海] 提交于 2019-12-03 12:08:28
问题 I am playing with this example http://scala.sygneca.com/code/remoteactors to learn how remote actors work in Scala (2.8.0). In particular I slightly modified how the messages send by the actors are defined as it follows: sealed trait Event extends Serializable case object Ping extends Event case object Pong extends Event case object Quit extends Event and everything works as expected. Unfortunately if I define the events as case classes instead of case objects as in: sealed trait Event

How to avoid scala's case class default toString function being overridden?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 11:22:27
Scala case class has a default toString function. But when this case class extends a trait with an existing toString() function, it will be rendered useless. How can I prevent this situation? tribbloid OK here is the easist answer: override def toString = ScalaRunTime._toString(this) end of story:) Here's a workaround I think may work, it may be too much ceremony, you decide. It involves a trait . trait StandardToString { this:Product => override def toString = productPrefix + productIterator.mkString("(", ",", ")") } Now trying it with some samples: trait Human { override def toString() =

In Scala, how can I programmatically determine the name of the fields of a case class?

為{幸葍}努か 提交于 2019-12-03 04:02:35
In Scala, suppose I have a case class like this: case class Sample(myInt: Int, myString: String) Is there a way for me to obtain a Seq[(String, Class[_])] , or better yet, Seq[(String, Manifest)] , describing the case class's parameters? Jim Pivarski It's me again (two years later). Here's a different, different solution using Scala reflection. It is inspired by a blog post , which was itself inspired by a Stack Overflow exchange . The solution below is specialized to the original poster's question above. In one compilation unit (a REPL :paste or a compiled JAR), include scala-reflect as a

Why are case objects serializable and case classes not?

ぐ巨炮叔叔 提交于 2019-12-03 02:40:38
I am playing with this example http://scala.sygneca.com/code/remoteactors to learn how remote actors work in Scala (2.8.0). In particular I slightly modified how the messages send by the actors are defined as it follows: sealed trait Event extends Serializable case object Ping extends Event case object Pong extends Event case object Quit extends Event and everything works as expected. Unfortunately if I define the events as case classes instead of case objects as in: sealed trait Event extends Serializable case class Ping extends Event case class Pong extends Event case class Quit extends

Scala create group of elements

倖福魔咒の 提交于 2019-12-02 23:48:49
问题 I want to create a group of elements in a message as in below image Updated: case class Element(key:String;value:String) Message can be represented something like below case class Msg(field1:Element,field2:Group) Group ->represents the repeating group - I need help to define group and sub groups The element defines the key=value combination which is repeated in groups The following are some points Are the "fields" attributes of a FixMessage? -Yes they are attributes of a Fix Message and each

How to reproduce case class behaviour with apply/unapply methods?

て烟熏妆下的殇ゞ 提交于 2019-12-02 03:27:31
问题 I tried to replace case class with mundane class and companion object and suddenly get type error. Code that compiles fine (synthetic example): trait Elem[A,B] { def ::[C](other : Elem[C,A]) : Elem[C,B] = other match { case Chain(head, tail) => Chain(head, tail :: this) case simple => Chain(simple, this) } } class Simple[A,B] extends Elem[A,B] final case class Chain[A,B,C](head : Elem[A,B], tail : Elem[B,C]) extends Elem[A,C] Change the last definition with: final class Chain[A,B,C](val head

How to define case classes with members with unbound type parameters?

若如初见. 提交于 2019-12-01 17:06:49
Given a class definition with bound type parameter Animal[A <: String] it seems that the Scala compiler does not infer B <: String from Animal[B] . Is the inference allowed? How to help the compiler to do the inference? Below is a concrete example with case classes where the lack of this inference is a problem. Consider the following case class hierarchy: sealed trait Person[+T <: Person[T]] case class Student() extends Person[Student] case class Professor() extends Person[Professor] I need to define a case class University which I can instantiate with a variable of type Person[_] , for

Mapping json to case class with Spark (spaces in the field name)

∥☆過路亽.° 提交于 2019-12-01 11:28:48
I am trying to read a json file with the spark Dataset API, the problem is that this json contains spaces in some of the field names. This would be a json row {"Field Name" : "value"} My case class needs to be like this case class MyType(`Field Name`: String) Then I can load the file into a DataFrame and it will load the correct schema val dataframe = spark.read.json(path) The problem comes when I try to convert the DataFrame to a Dataset[MyType] dataframe.as[MyType] The StructSchema loaded by the Encoder[MyType] is wrong and it introduces $u0020 instead of the space and I get the following

Convert Nested Case Classes to Nested Maps in Scala

感情迁移 提交于 2019-12-01 10:50:58
I have two nested case classes: case class InnerClass(param1: String, param2: String) case class OuterClass(myInt: Int, myInner: InnerClass) val x = OuterClass(11, InnerClass("hello", "world")) Which I want to convert to nested Maps of type Map[String,Any] so that I get something like this: Map(myInt -> 11, myInner -> Map(param1 -> hello, param2 -> world)) Of course, the solution should be generic and work for any case class. Note: This discussion gave a good answer on how to map a single case class to a Map. But I couldn't adapt it to nested case classes. Instead I get: Map(myInt -> 11,

Merge two case class of same type, except some fields

五迷三道 提交于 2019-12-01 06:53:36
问题 If you have a case class like: case class Foo(x: String, y: String, z: String) And you have two instances like: Foo("x1","y1","z1") Foo("x2","y2","z2") Is it possible to merge instance 1 in instance 2, except for field z, so that the result would be: Foo("x1","y1","z2") My usecase is just that I give JSON objects to a Backbone app through a Scala API, and the Backbone app gives me back a JSON of the same structure so that I can save/update it. These JSON objects are parsed as case class for