case-class

How To Access access Case class field Value from String name of the field

蹲街弑〆低调 提交于 2019-11-30 20:10:44
How should I extract the value of a field of a case class from a given String value representing the field. For example: case class Person(name: String, age: Int) val a = Person("test",10) Now here given a string name or age i want to extract the value from variable a . How do i do this? I know this can be done using reflection but I am not exactly sure how? Yuval Itzchakov What you're looking for can be achieve using Shapeless lenses. This will also put the constraint that a field actually exists on a case class at compile time rather than run time: import shapeless._ case class Person(name:

Replacing case class inheritance with extractors preserving exhaustiveness checks in Scala

≡放荡痞女 提交于 2019-11-30 18:57:25
I have a simple class hierarchy that represents a graph-like structure with several distinct types of vertexes implemented using case classes: sealed trait Node sealed abstract case class Vertex extends Node case class Arc extends Node case class VertexType1 (val a:Int) extends Vertex case class VertexType2 (val b:Int) extends Vertex This allows me to write match blocks like this: def test (x: Node) = x match { case _ : Arc => "got arc" case _ : Vertex => "got vertex" } or like this: def test (x: Node) = x match { case _ : Arc => "got arc" case c : Vertex => c match { case _ : VertexType1(a) =

Case class constructor argument type depending on the previous argument value

倾然丶 夕夏残阳落幕 提交于 2019-11-30 17:43:37
I am trying to do the following trait Stateful { type State } case class SystemState(system: Stateful, state: system.State) // does not compile That is, the type of state depends on (the value of) system . That, however, is not supported: illegal dependent method type: parameter appears in the type of another parameter in the same section or an earlier one With function arguments, I could split the arguments into two argument lists, which is not possible with a case class constructor: def f(system: Stateful)(state: system.State): Unit = {} // compiles The best I can do is: case class

Scala: Parse JSON directly into a case class

坚强是说给别人听的谎言 提交于 2019-11-30 11:48:13
问题 Given a string of JSON, and a case class that corresponds to it, what's a simple way to parse the JSON into the case class? There are many libraries available, but it seems that Scala might now do this out of the box. What about if the JSON should be parsed into a list of the case class? UPDATE: Jerkson seems to be abandoned, and I don't want to install the full Play or Lift framework or anything else heavy. 回答1: There are several frameworks which can exactly do that. circe Used a lot nowdays

hashCode in case classes in Scala

时光总嘲笑我的痴心妄想 提交于 2019-11-30 10:24:21
问题 I've read that Scala'a case class construct automatically generates a fitting equals and hashCode implementation. What does exactly the generated code look like? 回答1: As my professor used to say, only the code tells the truth! So just take a look at the code that is generated for: case class A(i: Int, s: String) We can instruct the Scala compiler to show us the generated code after the different phases, here after the typechecker: % scalac -Xprint:typer test.scala [[syntax trees at end of

Using .tupled method when companion object is in class

十年热恋 提交于 2019-11-30 06:25:59
问题 I am in the process of migrating from Slick to Slick 2, and in Slick 2 you are meant to use the tupled method when projecting onto a case class (as shown here http://slick.typesafe.com/doc/2.0.0-RC1/migration.html) The problem is when the case class has a companion object, i.e. if you have something like this case class Person(firstName:String,lastName:String) { } Along with a companion object object Person { def something = "rawr" } In the same scope, the tupled method no longer works,

Scala - how to print case classes like (pretty printed) tree

安稳与你 提交于 2019-11-30 06:20:47
问题 I'm making a parser with Scala Combinators. It is awesome. What I end up with is a long list of entagled case classes, like: ClassDecl(Complex,List(VarDecl(Real,float), VarDecl(Imag,float))) , just 100x longer. I was wondering if there is a good way to print case classes like these in a tree-like fashion so that it's easier to read..? (or some other form of Pretty Print ) ClassDecl name = Complex fields = - VarDecl name = Real type = float - VarDecl name = Imag type = float ^ I want to end up

How To Access access Case class field Value from String name of the field

 ̄綄美尐妖づ 提交于 2019-11-30 04:10:18
问题 How should I extract the value of a field of a case class from a given String value representing the field. For example: case class Person(name: String, age: Int) val a = Person("test",10) Now here given a string name or age i want to extract the value from variable a . How do i do this? I know this can be done using reflection but I am not exactly sure how? 回答1: What you're looking for can be achieve using Shapeless lenses. This will also put the constraint that a field actually exists on a

Replacing case class inheritance with extractors preserving exhaustiveness checks in Scala

强颜欢笑 提交于 2019-11-30 03:15:04
问题 I have a simple class hierarchy that represents a graph-like structure with several distinct types of vertexes implemented using case classes: sealed trait Node sealed abstract case class Vertex extends Node case class Arc extends Node case class VertexType1 (val a:Int) extends Vertex case class VertexType2 (val b:Int) extends Vertex This allows me to write match blocks like this: def test (x: Node) = x match { case _ : Arc => "got arc" case _ : Vertex => "got vertex" } or like this: def test

Scala: Parse JSON directly into a case class

拜拜、爱过 提交于 2019-11-30 01:22:43
Given a string of JSON, and a case class that corresponds to it, what's a simple way to parse the JSON into the case class? There are many libraries available, but it seems that Scala might now do this out of the box. What about if the JSON should be parsed into a list of the case class? UPDATE: Jerkson seems to be abandoned, and I don't want to install the full Play or Lift framework or anything else heavy. There are several frameworks which can exactly do that. circe Used a lot nowdays. Many great features. Will pull cats in. https://circe.github.io/circe/ https://github.com/circe/circe