FAQ Database Discussion Community


Error while Parsing json into scala case class

java,scala,jackson,case-class
In my spring(mvc) web application, I am using org.codehaus.jackson.map.ObjectMapper in my scala code to map my json to scala objects using case classes. My Json String is an array of json objects objects. so I am using: val user = mapper.readValue(myJson, classOf[List[MyClass]]) This line throws an error: Exception in thread...

How to access fields of case class from JSP?

scala,jsp,jstl,el,case-class
When I was a Java programmer my typical approach was to write a POJO with getters and access its fields in JSP through those getters ${pojo.field}. Now I'm trying to use Scala. Case classes looks like a good replacement for POJO's, but scala provides field() getters instead of getField() which...

Scala Case Class Tupled

scala,case-class
How can I call the tupled method on this case class? case class(a: Int, b: String)(c: String, d: Int) The reason why I have my case class like this is because I want to use only the first two parameters to be considered for equals and hashCode comparison! So how...

Scala case classes test

scala,testing,case-class
I define a sealed trait shape with case class Point and LineSegment and have a behaviour for the class LineSegment which tells me if the two LineSegments intersect, however when I try to test that behavior.. it says it cannot recognize it in the test suite. sealed trait Shape case...

How to decorate an immutable object graph from scala case classes

json,scala,playframework,immutability,case-class
I'm reading structured JSON, using Play Frameworks' JSON Reads to build up an object graph with case classes. An example: case class Foo ( id: Int, bar_id: Int, baz_id: Int, x: Int, y: String ) { var bar: Bar = null var baz: Baz = null } After building the...

Scala: How to pattern-match the enclosing object of an inner case class?

scala,pattern-matching,case-class
I have an inner case class, specifically an event from this question, and want to match it, including the outer object: class Player { var _life = 20 def life = _life def gainLife(life: Int) = execute(GainLife(life)) case class GainLife(life: Int) extends Event { def execute() = _life += life...

How to initialize one field using another in case class?

scala,case-class
Suppose I have a case class like case class Person(fname:String, lname:String, nickName:Option[String] = None) On creating an instance like Person("John", "Doe"), I want nickName to be automatically assigned to fname, if one is not given. Eg: val p1 = Person("John", "Doe") p1.nickName.get == "John" val p2 = Person("Jane", "Doe", "Joe")...

Converting a case class to CSV in Scala

scala,csv,macros,case-class
Is there a elegant way to convert a case class to a CSV value. For example - case class Person( name : String, age : Int, gender: String, address : Option[String]) I was thinking about using macros but would like to know if there are any other alternatives. NOTE: The...

== for case class and “non-case” class in Scala

scala,case-class
I am studying Scala and ran into the following puzzle. I can define the following case classes: abstract class Expr case class Number(n: Int) extends Expr When I create two instances from the class Number and compare them val x1 = Number(1) val x2 = Number(1) x1 == x2 I...

Case class constructor argument type depending on the previous argument value

scala,case-class,dependent-type,dotty
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 statedepends on (the value of) system. That, however, is not supported: illegal dependent method type: parameter appears in the type of another...

Scala: How to compute the sum of all elements in the leaves of a Binary Tree?

scala,sum,binary-tree,immutability,case-class
I'm learning Scala by working the exercises from the book "Scala for the Impatient". Given the following way to model binary trees with case classes, one question asks to compute the sum of all elements in the leaves. sealed abstract class BinaryTree case class Leaf(value: Int) extends BinaryTree case class...