case-class

Case class to map in Scala

孤者浪人 提交于 2019-11-26 23:38:23
Is there a nice way I can convert a Scala case class instance, e.g. case class MyClass(param1: String, param2: String) val x = MyClass("hello", "world") into a mapping of some kind, e.g. getCCParams(x) returns "param1" -> "hello", "param2" -> "world" Which works for any case class, not just predefined ones. I've found you can pull the case class name out by writing a method that interrogates the underlying Product class, e.g. def getCCName(caseobj: Product) = caseobj.productPrefix getCCName(x) returns "MyClass" So I'm looking for a similar solution but for the case class fields. I'd imagine a

Should I use the final modifier when declaring case classes?

风流意气都作罢 提交于 2019-11-26 22:41:05
问题 According to scala-wartremover static analysis tool I have to put "final" in front of every case classes I create: error message says "case classes must be final". According to scapegoat (another static analysis tool for Scala) instead I shouldn't (error message: "Redundant final modifier on case class") Who is right, and why? 回答1: It is not redundant in the sense that using it does change things. As one would expect, you cannot extend a final case class, but you can extend a non-final one.

In Scala, is there an easy way to convert a case class into a tuple?

匆匆过客 提交于 2019-11-26 22:36:18
问题 Is there an easy way to convert a case class into a tuple? I can, of course, easily write boilerplate code to do this, but I mean without the boilerplate. What I'm really after is a way to easily make a case class lexicographically Ordered. I can achieve the goal for tuples by importing scala.math.Ordering.Implicits._, and voila, my tuples have an Ordering defined for them. But the implicits in scala.math.Ordering don't work for case classes in general. 回答1: How about calling unapply().get in

Why is parameter in contravariant position?

北慕城南 提交于 2019-11-26 22:15:38
问题 I'm trying to use a covariant type parameter inside a trait to construct a case-class like so: trait MyTrait[+T] { private case class MyClass(c: T) } compiler says: error: covariant type T occurs in contravariant position in type T of value c I then tried the following but it also didn't work: trait MyTrait[+T] { private case class MyClass[U <: T](c: U) } the error this time is: error: covariant type T occurs in contravariant position in type >: Nothing <: T of type U Could somebody explain

Why do case class companion objects extend FunctionN?

左心房为你撑大大i 提交于 2019-11-26 19:45:36
问题 When you create a case class, the compiler creates a corresponding companion object with a few of the case class goodies: an apply factory method matching the primary constructor, equals , hashCode , and copy . Somewhat oddly, this generated object extends FunctionN. scala> case class A(a: Int) defined class A scala> A: (Int => A) res0: (Int) => A = <function1> This is only the case if: There is no manually defined companion object There is exactly one parameter list There are no type

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

≯℡__Kan透↙ 提交于 2019-11-26 18:41:38
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? VonC 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 case class fields, please see https://stackoverflow.com/a/55498135/1586965 This may lead you to think

How to override apply in a case class companion

社会主义新天地 提交于 2019-11-26 12:51:51
So here's the situation. I want to define a case class like so: case class A(val s: String) and I want to define an object to ensure that when I create instances of the class, the value for 's' is always uppercase, like so: object A { def apply(s: String) = new A(s.toUpperCase) } However, this doesn't work since Scala is complaining that the apply(s: String) method is defined twice. I understand that the case class syntax will automatically define it for me, but isn't there another way I can achieve this? I'd like to stick with the case class since I want to use it for pattern matching. The

Scala case class inheritance

感情迁移 提交于 2019-11-26 12:36:15
问题 I have an application based on Squeryl. I define my models as case classes, mostly since I find convenient to have copy methods. I have two models that are strictly related. The fields are the same, many operations are in common, and they are to be stored in the same DB table. But there is some behaviour that only makes sense in one of the two cases, or that makes sense in both cases but is different. Until now I only have used a single case class, with a flag that distinguishes the type of

Mixing in a trait dynamically

ⅰ亾dé卋堺 提交于 2019-11-26 12:21:54
问题 Having a trait trait Persisted { def id: Long } how do I implement a method that accepts an instance of any case class and returns its copy with the trait mixed in? The signature of the method looks like: def toPersisted[T](instance: T, id: Long): T with Persisted 回答1: This can be done with macros (that are officially a part of Scala since 2.10.0-M3). Here's a gist example of what you are looking for. 1) My macro generates a local class that inherits from the provided case class and Persisted

Overload constructor for Scala&#39;s Case Classes?

依然范特西╮ 提交于 2019-11-26 12:12:48
问题 In Scala 2.8 is there a way to overload constructors of a case class? If yes, please put a snippet to explain, if not, please explain why? 回答1: Overloading constructors isn't special for case classes: case class Foo(bar: Int, baz: Int) { def this(bar: Int) = this(bar, 0) } new Foo(1, 2) new Foo(1) However, you may like to also overload the apply method in the companion object, which is called when you omit new . object Foo { def apply(bar: Int) = new Foo(bar) } Foo(1, 2) Foo(1) In Scala 2.8,