case-class

Using Play Framework and case class with greater than 22 parameters

萝らか妹 提交于 2019-12-24 03:16:07
问题 I have seen some of the other issues involving the infamous "22 fields/parameters" issue that is an inherent bug (feature?) of Scala V < 2.11. See here and here. However, as per this blog post, it appears that the 22 parameter limit in case class has been fixed; at least where the language is concerned. I have a case class that I want to load an arbitrary (Read: > 22) number of values into which will later be read into a JSON object using the Play library. It looks something like this: object

Spark Dataframe schema definition using reflection with case classes and column name aliases

和自甴很熟 提交于 2019-12-23 20:30:26
问题 I ran into a little problem with my Spark Scala script. Basically I have raw data which I am doing aggregations on and after grouping and counting etc I want to save the output to a specific JSON format. EDIT: I tried to simplify the question and rewrote it: When I select data from the source dataframe with an Array[org.apache.spark.sql.Column] where the column names have aliases, then using column names (or indeed indices) as variables when trying to map the rows to a case class, then I get

How to pattern match abstract parent classes in a inheritance tree

自作多情 提交于 2019-12-23 19:25:39
问题 I am new to scala with a java background. Is there a way to pattern match super classes (or traits) in a class inheritance tree with leafs as case classes and nodes abstract classes or traits? As far as I know case class inheritance is not allowed. I think that pattern matching abstract classes in large inheritance tree would be very helpful In the following code the last case in the match statement errors during compilation sealed trait Person { def name: String } case class Customer(name:

Efficient map with case class as a key in Scala?

穿精又带淫゛_ 提交于 2019-12-23 15:16:23
问题 A following C code uses enum and array as efficient "map" from enum to anything: enum Color { ColorRed, ColorGreen, ColorBlue, ColorSize}; void f() { int x[ColorSize]; x[ColorRed] = 12; x[ColorGreen] = 33; x[ColorBlue] = 4; return x[ColorGreen]; } Is this possible with Scala? I.e. to have a "map" from case class to something, implemented as efficient array and not as tree or as hashmap. Yet I would like to be able to index only with a paricular type not with Int. Update: In short I would like

Jackson mapper with generic class in scala

寵の児 提交于 2019-12-23 10:54:55
问题 I am trying to serialise GeneralResponse : case class GeneralResponse[T](succeeded: Boolean, payload: Option[T]) and the payload is GroupsForUserResult : case class GroupsForUserResult(groups: Seq[UUID]). I am using mapper.readValue(response.body, classOf[GeneralResponse[GroupsForUserResult]]) but unfortunately the payload is serialised as a Map and not as the desired case class ( GroupForUserResult ). 回答1: Because of Java Erasure - Jackson can't know at runtime about the generic type T from

Equality and Case Classes

喜欢而已 提交于 2019-12-23 04:53:29
问题 I have: sealed trait BEValue case class BEByteString(val value: Array[Byte]) extends BEValue { def equals(that: BEByteString): Boolean = this.value.deep == that.value.deep def ==(that: BEByteString) = this equals that } case class BEList(val value: List[BEValue]) extends BEValue BEByteString("spam".getBytes) == BEByteString("spam".getBytes) //true val l1 = BEList(BEByteString("spam".getBytes):: Nil) val l2 = BEList(BEByteString("spam".getBytes):: Nil) l1 == l2 // false. Why ? 回答1: You should

I need a specific example of how to define a local parameter in the primary constructor of an immutable _case_ class

﹥>﹥吖頭↗ 提交于 2019-12-22 12:02:15
问题 I have normal Scala class I am wanting to refactor to become an immutable case class. As I'm needing the class to be well-behaved in Set operations, I want all the Scala compiler automatically generated methods provided on a case class. IOW, I am wanting to avoid having to write these various methods; equals , hashCode , toString , etc., as that is very error prone. And I am needing to do this for a raft of classes, so I need a general solution, not just a specific solution anomalous quick

How to resolve scala.MatchError when creating a Data Frame

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 08:12:46
问题 I have text file which has complex structured row. I am using customer converter which converts the given string(line) to Pojo class(countryInfo). After converting, I am building DF. The POJO class has a field which is a List of Custome Type(GlobalizedPlayTimeWindows). I created a Struct which matches this GlobalizedPlayTimeWindows and trying to convert the existing Custom Type to the Struct but keep getting error. StructType I created : import org.apache.spark.sql.types._ val PlayTimeWindow

Will the var members in case class affect case class's equality?

梦想与她 提交于 2019-12-21 11:01:14
问题 I have made heavy use of case classes in my code, replying on the underlying equality definitions of case class to behave correctly. Then now I found that I need to add another field member to a case class. So if I add a var field member in case class, will it mess up the equality attributes for the case class? If 1 is yes, then what if I just change the var field value once, after that, no any reassignment will happen, before the case class goes into any collections or do equality comparison

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

◇◆丶佛笑我妖孽 提交于 2019-12-19 18:27:30
问题 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