case-class

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

随声附和 提交于 2019-11-27 17:48:37
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. S-C How about calling unapply().get in the companion object? case class Foo(foo: String, bar: Int) val (str, in) = Foo.unapply(Foo("test", 123

What are the disadvantages to declaring Scala case classes?

五迷三道 提交于 2019-11-27 16:45:08
If you're writing code that's using lots of beautiful, immutable data structures, case classes appear to be a godsend, giving you all of the following for free with just one keyword: Everything immutable by default Getters automatically defined Decent toString() implementation Compliant equals() and hashCode() Companion object with unapply() method for matching But what are the disadvantages of defining an immutable data structure as a case class? What restrictions does it place on the class or its clients? Are there situations where you should prefer a non-case class? Dave Griffith One big

Map flatten and flatmap not equivalent

折月煮酒 提交于 2019-11-27 16:06:13
问题 I thought that Scala construct map(f).flatten was equivalent to flatMap(f). But with this example, it is not the case. I wonder what is the role of the case class in it. If I use integers, both are equivalent. But in my case, I cannot. case class CTest(v: Int) val s = Set(Map(CTest(0) -> List(0, 3), CTest(1) -> List(0, 2))) val possibilities = s flatMap { m => val mapping = m flatMap { case (label, destNodes) => destNodes map { case nodes => (label, nodes) } } mapping } possibilities Yields

How is this case class match pattern working?

青春壹個敷衍的年華 提交于 2019-11-27 14:24:04
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 : Daniel C. Sobral When doing pattern matching, the Scala compiler will interpret o1 c1 o2 the same as c1(o1, o2) . That's why :: works inside pattern matches too. 来源: https://stackoverflow.com/questions

rely on methods of case class in trait

妖精的绣舞 提交于 2019-11-27 13:58:12
问题 Is there a way to rely on methods defined in case class in a trait? E.g., copy: the following doesn't work. I'm not sure why, though. trait K[T <: K[T]] { val x: String val y: String def m: T = copy(x = "hello") def copy(x: String = this.x, y: String = this.y): T } case class L(val x: String, val y: String) extends K[L] Gives: error: class L needs to be abstract, since method copy in trait K of type (x: String,y: String)L is not defined case class L(val x: String, val y: String) extends K[L]

“dynamically” creating case classes with macros

こ雲淡風輕ζ 提交于 2019-11-27 12:12:32
I would like to create a macro generated hierarchy of sealed abstract and case classes. There was an example similar to this with http://docs.scala-lang.org/overviews/macros/typemacros.html but is is now obsolete. Is this still possible? I think it would be incredibly powerful to generate a type safe AST for some specified grammar. Ideally with an IDE able to resolve all the classes. Travis Brown First for some shameless self-promotion: Eugene Burmako and I are giving a talk on type providers , a closely related topic, at Scalar 2014 tomorrow, and I encourage you to take a look at the example

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

限于喜欢 提交于 2019-11-27 11:47:57
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 } Travis Brown MethodSymbol has an isCaseAccessor method that allows you to do precisely this: def getMethods[T: TypeTag] = typeOf[T].members.collect { case m: MethodSymbol if m.isCaseAccessor => m }.toList Now you can write the

Easy idiomatic way to define Ordering for a simple case class

懵懂的女人 提交于 2019-11-27 10:07:11
I have a list of simple scala case class instances and I want to print them in predictable, lexicographical order using list.sorted , but receive "No implicit Ordering defined for ...". Is there exist an implicit that provides lexicographical ordering for case classes? Is there simple idiomatic way to mix-in lexicographical ordering into case class? scala> case class A(tag:String, load:Int) scala> val l = List(A("words",50),A("article",2),A("lines",7)) scala> l.sorted.foreach(println) <console>:11: error: No implicit Ordering defined for A. l.sorted.foreach(println) ^ I am not happy with a

Constructing simple Scala case classes from Strings, strictly without boiler-plate

谁说胖子不能爱 提交于 2019-11-27 09:35:14
问题 I seek succinct code to initialize simple Scala case classes from Strings (e.g. a csv line): case class Person(name: String, age: Double) case class Book(title: String, author: String, year: Int) case class Country(name: String, population: Int, area: Double) val amy = Creator.create[Person]("Amy,54.2") val fred = Creator.create[Person]("Fred,23") val hamlet = Creator.create[Book]("Hamlet,Shakespeare,1600") val finland = Creator.create[Country]("Finland,4500000,338424") What's the simplest

IntelliJ Scala Plugin's case class indentation is absurd

梦想的初衷 提交于 2019-11-27 09:04:55
问题 When a case class has many fields and their names are long, it is often a good idea to write each field in each line like: case class Person ( name: String, age: Int ) This resembles C/C++ struct definition and totally readable even when the case class becomes bigger. But IntelliJ IDEA's default Scala plugin automatically changes its indentation: case class Person ( name: String, age: Int ) which looks weird to me, but the Scala Style Guide doesn't mention anything about case class