case-class

Overload constructor for Scala's Case Classes?

女生的网名这么多〃 提交于 2019-11-27 06:51:15
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? 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, named and default parameters can often be used instead of overloading. case class Baz(bar: Int, baz: Int = 0

Why is parameter in contravariant position?

自古美人都是妖i 提交于 2019-11-27 06:41:17
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 the T is in a covariant position here and suggest a solution for this problem? Thx! This is a

Why were the case classes without a parameter list deprecated?

泪湿孤枕 提交于 2019-11-27 05:35:08
问题 Why were the case classes without a parameter list deprecated from Scala? And why does compiler suggest to use () as parameter list instead? EDIT : Someone please answer my second question... :| 回答1: It is really easy to accidentally use a no-arg case class incorrectly as a pattern. scala> case class Foo warning: there were deprecation warnings; re-run with -deprecation for details defined class Foo scala> (new Foo: Any) match { case Foo => true; case _ => false } res10: Boolean = false

What is *so* wrong with case class inheritance?

烈酒焚心 提交于 2019-11-27 03:10:26
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 ? One word: equality case classes come with a supplied implementation of equals and hashCode . The equivalence relation, known as equals works like this (i.e. must have the following properties): For all x ; x equals x is true (reflexive) For x , y

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

泄露秘密 提交于 2019-11-27 03:01:30
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", 25, List("Lorem", "Ipsum")) myUser.toMap should return Map("name" -> "Foo", "age" -> 25, "posts" ->

Reflection on a Scala case class

半城伤御伤魂 提交于 2019-11-27 02:22:29
问题 I'm trying to write a trait (in Scala 2.8) that can be mixed in to a case class, allowing its fields to be inspected at runtime, for a particular debugging purpose. I want to get them back in the order that they were declared in the source file, and I'd like to omit any other fields inside the case class. For example: trait CaseClassReflector extends Product { def getFields: List[(String, Any)] = { var fieldValueToName: Map[Any, String] = Map() for (field <- getClass.getDeclaredFields) {

How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?

[亡魂溺海] 提交于 2019-11-27 01:59:22
问题 I have a case class defined as such: case class StreetSecondary(designator: String, value: Option[String]) I then define an explicit companion object: object StreetSecondary { //empty for now } The act of defining the explicit companion object StreetSecondary causes the compiler produced "implicit companion object" to be lost; i.e. replaced with no ability to access the compiler produced version. For example, the tupled method is available on case class StreetSecondary via this implicit

What is the Scala case class equivalent in PySpark?

蓝咒 提交于 2019-11-27 01:55:57
问题 How would you go about employing and/or implementing a case class equivalent in PySpark? 回答1: As mentioned by Alex Hall a real equivalent of named product type, is a namedtuple . Unlike Row , suggested in the other answer, it has a number of useful properties: Has well defined shape and can be reliably used for structural pattern matching: >>> from collections import namedtuple >>> >>> FooBar = namedtuple("FooBar", ["foo", "bar"]) >>> foobar = FooBar(42, -42) >>> foo, bar = foobar >>> foo 42

Safely copying fields between case classes of different types

旧城冷巷雨未停 提交于 2019-11-27 01:46:30
问题 Assuming you have case classes like the following case class Test1(a:String,b:Int,c:Char) case class Test2(a:String,b:Int) And you instantiate the classes with the following variables val test1 = Test1("first",2,'3') val test2 = Test2("1st",20) Is there a way to use the .copy method (or otherwise), to apply the variables inside Test2 to Test1, something like val test3 = test1.copy(test2) //Note this isn't valid scala code // Result should be ("1st",20,'3') If this isn't possible in pure scala

Mixing in a trait dynamically

a 夏天 提交于 2019-11-27 00:05:44
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 Eugene Burmako 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, much like new T with Persisted would do. Then it caches its argument (to prevent multiple