case-class

Convert Nested Case Classes to Nested Maps in Scala

怎甘沉沦 提交于 2019-12-19 10:22:00
问题 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

Extract label values from a LabelledGeneric instance

非 Y 不嫁゛ 提交于 2019-12-18 03:55:49
问题 Consider the following example: import shapeless._ case class Foo(bar: String, baz: Boolean) val labl = LabelledGeneric[Foo] Now, the type of labl is (prettified) LabelledGeneric[Foo] { type Repr = FieldType[Symbol @@ String("bar"), String] :: FieldType[Symbol @@ String("baz"), Boolean] :: HNil } which already conveys the information I need, i.e. the names of the case class fields. What I'm looking for is a way to go from labl to something along the lines of "bar" :: "baz" :: HNil i.e.

Scala case class extending Product with Serializable

房东的猫 提交于 2019-12-17 21:56:28
问题 I am learning scala and tried following form Scala Cookbook: trait Animal trait FurryAnimal extends Animal case class Dog(name:String) extends Animal case class Cat(name:String) extends Animal Now when I did following as : val x = Array(Dog("Fido"),Cat("Felix")) it show result as : x:Array[Product with Serializable with Animal] = Array(Dog(Fido),Cat(Felix)) Although I know that a case class is mixed in with Product trait What I am not getting is : Product with Serializable with Animal As per

How is this case class match pattern working?

陌路散爱 提交于 2019-12-17 11:45:49
问题 I've just seen this case class in the Scala actors package: case class ! [a](ch: Channel[a], msg: a) And in the JavaDoc it describes usage in the following form: receive { case Chan1 ! msg1 => ... case Chan2 ! msg2 => ... } Why is this not: receive { case !(Chan1, msg1) => ... case !(Chan2, msg2) => ... } Is the bang operator ! a special case in a similar way to methods ending in a colon : 回答1: When doing pattern matching, the Scala compiler will interpret o1 c1 o2 the same as c1(o1, o2) .

Scala 2.10 reflection, how do I extract the field values from a case class, i.e. field list from case class

戏子无情 提交于 2019-12-17 10:26:47
问题 How can I extract the field values from a case class in scala using the new reflection model in scala 2.10? For example, using the below doesn't pull out the field methods def getMethods[T:TypeTag](t:T) = typeOf[T].members.collect { case m:MethodSymbol => m } I plan to pump them into for {field <- fields} { currentMirror.reflect(caseClass).reflectField(field).get } 回答1: MethodSymbol has an isCaseAccessor method that allows you to do precisely this: def getMethods[T: TypeTag] = typeOf[T]

What is *so* wrong with case class inheritance?

杀马特。学长 韩版系。学妹 提交于 2019-12-17 07:11:56
问题 While looking for something else, quite out of mere coincidence I stumbled upon few comments about how diabolical case class inheritance is. There was this thing called ProductN , wretches and kings, elves and wizards and how some kind of a very desirable property is lost with case classes inheritance. So what is so wrong with case class inheritance ? 回答1: One word: equality case classes come with a supplied implementation of equals and hashCode . The equivalence relation, known as equals

Scala Macros: Making a Map out of fields of a class in Scala

只谈情不闲聊 提交于 2019-12-17 07:07:52
问题 Let's say that I have a lot of similar data classes. Here's an example class User which is defined as follows: case class User (name: String, age: Int, posts: List[String]) { val numPosts: Int = posts.length ... def foo = "bar" ... } I am interested in automatically creating a method ( at compile time ) that returns a Map in a way that each field name is mapped to its value when it is called in runtime. For the example above, let's say that my method is called toMap : val myUser = User("Foo",

How to get around the Scala case class limit of 22 fields?

☆樱花仙子☆ 提交于 2019-12-17 04:26:22
问题 Scala case classes have a limit of 22 fields in the constructor. I want to exceed this limit, is there a way to do it with inheritance or composition that works with case classes? 回答1: More recently (Oct 2016, six years after the OP), the blog post "Scala and 22" from Richard Dallaway explores that limit: Back in 2014, when Scala 2.11 was released, an important limitation was removed: Case classes with > 22 parameters are now allowed. That said, there still exists a limit on the number of

How to define schema for custom type in Spark SQL?

纵饮孤独 提交于 2019-12-17 02:26:07
问题 The following example code tries to put some case objects into a dataframe. The code includes the definition of a case object hierarchy and a case class using this trait: import org.apache.spark.{SparkContext, SparkConf} import org.apache.spark.sql.SQLContext sealed trait Some case object AType extends Some case object BType extends Some case class Data( name : String, t: Some) object Example { def main(args: Array[String]) : Unit = { val conf = new SparkConf() .setAppName( "Example" )

What is the difference between Scala's case class and class?

百般思念 提交于 2019-12-17 01:20:34
问题 I searched in Google to find the differences between a case class and a class . Everyone mentions that when you want to do pattern matching on the class, use case class. Otherwise use classes and also mentioning some extra perks like equals and hash code overriding. But are these the only reasons why one should use a case class instead of class? I guess there should be some very important reason for this feature in Scala. What is the explanation or is there a resource to learn more about the