case-class

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

邮差的信 提交于 2019-11-28 15:48:57
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 Creator object to do this? I would learn a lot about Scala from seeing a good solution to this. (Note

IntelliJ Scala Plugin's case class indentation is absurd

隐身守侯 提交于 2019-11-28 15:14:15
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 indentation. I couldn't find anything in the IDE settings that can change this behaviour. Is there an option to

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-28 09:24:17
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 companion object. However, once I define the explicit companion object, the tupled method is "lost". So,

What is the Scala case class equivalent in PySpark?

半腔热情 提交于 2019-11-28 07:40:58
How would you go about employing and/or implementing a case class equivalent in PySpark? 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 >>> bar -42 In contrast Rows are not reliable when used with keyword arguments : >>> from pyspark.sql

Safely copying fields between case classes of different types

我是研究僧i 提交于 2019-11-28 07:02:37
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, how would it be done in Shapeless 1/2 (current code is in Shapeless 1, however we are planning to

Why were the case classes without a parameter list deprecated?

本秂侑毒 提交于 2019-11-28 05:42:09
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... :| 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 Instead of: scala> (new Foo: Any) match { case _: Foo => true; case _ => false } res11: Boolean = true Or better:

How to use scala macros to create a function object (to create a Map[String, (T) => T])

ε祈祈猫儿з 提交于 2019-11-28 05:21:57
问题 I am trying to use Scala macros to create a case class map of single-parameter copy methods, with each method accepting a Play Json JsValue and a case class instance, and returning an updated copy of the instance. However, I am running into problems with the macro syntax for returning a function object. Given a case class case class Clazz(id: Int, str: String, strOpt: Option[String]) the intention is to create a map of the class's copy methods implicit def jsonToInt(json: JsValue) = json.as

How can I create an instance of a Case Class with constructor arguments with no Parameters in Scala?

被刻印的时光 ゝ 提交于 2019-11-28 01:46:25
问题 I'm making a Scala app that sets by reflection field values. This works OK. However, in order to set field values I need a created instance. If I have a class with an empty constructor, I can do this easily with classOf[Person].getConstructors.... However, when I try doing this with a Case class with a non empty constructor It doesn't work. I have all of the field names and its values, as well as the Object type I need to create. Can I instance the Case Class somehow with what I've got? The

Why do case class companion objects extend FunctionN?

若如初见. 提交于 2019-11-27 18:58:40
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 arguments The case class isn't abstract. Seems like this was added about two years ago. The latest incarnation

Should I use the final modifier when declaring case classes?

丶灬走出姿态 提交于 2019-11-27 18:28:14
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? 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. Why does wartremover suggest that case classes should be final? Well, because extending them isn't really a