case-class

make string using productiterator

别说谁变了你拦得住时间么 提交于 2019-12-11 05:57:09
问题 I want to write to String representation of case class Grp trait Value // define these in different files if you want case class Student(value: String) extends Value case class Employee(value: Double) extends Value case class Department(value: Int) extends Value case class Element(key: String, value: Value) case class Grp (elements: List[Element] = Nil) extends Value { def add (key: String, value: Value): Grp = Grp(this.elements ++ List(Element(key, value))) } val s= Grp() .add("2", Student(

spark-submit fails when case class fields are reserved java keywords with backticks

偶尔善良 提交于 2019-12-11 04:38:04
问题 I have backticks used for reserved keyword. One example for the case class is as follows: case class IPC( `type`: String, main: Boolean, normalized: String, section:String, `class`: String, subClass: String, group:String, subGroup: String ) I have declared the sparksession as follows: def run(params: SparkApp.Params): Unit ={ val sparkSession = SparkSession.builder.master("local[*]").appName("SparkUsptoParser").getOrCreate() // val conf = new SparkConf().setAppName("SparkUsptoParser").set(

Define 'copy' method in trait for case class

筅森魡賤 提交于 2019-12-11 03:53:24
问题 Given simplified code example: sealed trait A { val c1: String val c2: Int def copy[Z <: A](src: File) : Z } case class B(c1: String, c2: Int, src: File) extends A case class C(c1: String, c2: Int, c3: MyClass, src: File) extends A how do I define copy method in trait A so it will match generated one for case class and 'target' file? Given definition does typecheck and complains about missing method copy in classes B and C. 回答1: scala compiler will not generate copy methods for case class

construct case class from collection of parameters

本秂侑毒 提交于 2019-12-11 00:53:24
问题 Given: case class Thing(a:Int, b:String, c:Double) val v = Vector(1, "str", 7.3) I want something that will magically create: Thing(1, "str", 7.3) Does such a thing exist (for arbitrary size Things)? 回答1: My first time dipping my toes into the 2.10 experimental reflection facilities. So mostly following this outline http://docs.scala-lang.org/overviews/reflection/overview.html, I came up with this: import scala.reflect.runtime.{universe=>ru} case class Thing(a: Int, b: String, c: Double)

Tupled method for case class having a type parameter

十年热恋 提交于 2019-12-10 04:19:18
问题 When having a case class with a type parameter, I don't see how I can call the tupled method. It seems to be find with apply and unapply . scala> case class Foo[T](x:T, y:T) defined class Foo scala> Foo.apply[Int] _ res1: (Int, Int) => Foo[Int] = <function2> scala> Foo.unapply[Int] _ res2: Foo[Int] => Option[(Int, Int)] = <function1> scala> Foo.tupled[Int] _ <console>:10: error: value tupled is not a member of object Foo Foo.tupled[Int] _ ^ Any idea on what's going on? 回答1: tl;dr Companion

Create common trait for all case classes supporting copy(id=newId) method

浪尽此生 提交于 2019-12-09 16:48:27
问题 I'm trying to do something like that: trait IdentifiableModel[T] { self: { def copy(id: ObjectId): T } => val id: ObjectId } I've found some other related questions trying to do similar things but they didn't really answer to this question. In my case, I'm trying to copy the case class subclasses of IdentifiableModel that all share an id value 回答1: Travis Brown has the good answer but didn't reply. He uses Scala macros: Howto model named parameters in method invocations with Scala macros? 来源:

Scala getting field and type of field of a case class

二次信任 提交于 2019-12-09 16:01:19
问题 So I'm trying to get the field and their types in a case class. At the moment I am doing it like so typeOf[CaseClass].members.filter(!_.isMethod).foreach{ x => x.typeSignature match { case _:TypeOfFieldInCaseClass => do something case _:AnotherTypeOfFieldInCaseClass => do something } } the problem is x.typeSignature is of type reflect.runtime.universe.Type which cannot match on any of the types that reside in the case class. Is there some way to do this? 回答1: Let's say you have the following

How do I add a no-arg constructor to a Scala case class with a macro annotation?

自古美人都是妖i 提交于 2019-12-08 22:00:14
问题 I'm trying to answer this question. Instead of writing: case class Person(name: String, age: Int) { def this() = this("",1) } I thought I'd use macro annotations to expand it from: @Annotation case class Person(name: String, age: Int) So I tried adding the new constructor as a plain-old DefDef using quasiquotes in a macro annotation's impl, like: val newCtor = q"""def this() = this("", 1)""" val newBody = body :+ newCtor q"$mods class $name[..$tparams](..$first)(...$rest) extends ..$parents {

Efficiently serializing case classes

谁说我不能喝 提交于 2019-12-08 16:51:29
问题 For a library I'm working on, I need to provide an efficient, convenient and typesafe method of serializing scala classes. The ideal would be if a user can create a case class, and as long as all the members are serializable it should seemlessly be so too. I know precisely the type during both the serializing and deserializing stage so there's no need (and can not afford to) have any "schema" information as part of the seriazation format (like the Java object serialisation). I've been playing

Handling case classes in twitter chill (Scala interface to Kryo)?

大城市里の小女人 提交于 2019-12-06 23:47:37
问题 Twitter-chill looks like a good solution to the problem of how to serialize efficiently in Scala without excessive boilerplate. However, I don't see any evidence of how they handle case classes. Does this just work automatically or does something need to be done (e.g. creating a zero-arg constructor)? I have some experience with the WireFormat serialization mechanism built into Scoobi, which is a Scala Hadoop wrapper similar to Scalding. They have serializers for case classes up to 22