case-class

DSL in scala using case classes

故事扮演 提交于 2019-12-06 21:10:58
问题 My use case has case classes something like case class Address(name:String,pincode:String){ override def toString =name +"=" +pincode } case class Department(name:String){ override def toString =name } case class emp(address:Address,department:Department) I want to create a DSL like below.Can anyone share the links about how to create a DSL and any suggestions to achieve the below. emp.withAddress("abc","12222").withDepartment("HR") Update: Actual use case class may have more fields close to

Modifying case class constructor parameter before setting value

雨燕双飞 提交于 2019-12-06 20:30:50
问题 Is there a way in Scala to modify a parameter passed to a single-argument case class constructor / apply() method before it becomes a val ? E.g. case class AbsVal private(aVal: Double) object AbsVal { def apply(aVal: Double): AbsVal = AbsVal(Math.abs(aVal)) // doesn't compile } This fails of course with ambiguous reference to overloaded definition . I thought maybe I could trick it with named parameters (and different parameter names for the constructor vs apply() ), but that doesn't work

Case class companion object generation error for compound type

元气小坏坏 提交于 2019-12-06 20:12:03
问题 Defined empty trait Test: trait Test what used in compound type: scala> val a : Int with Test = 10.asInstanceOf[Int with Test] a: Int with Test = 10 and case class with parameter of compound type (like Unboxed Tagged Type): scala> case class Foo(a: Int with Test) error: type mismatch; found : Double required: AnyRef Note: an implicit exists from scala.Double => java.lang.Double, but methods inherited from Object are rendered ambiguous. This is to avoid a blanket implicit which would convert

I need a specific example of how to define a local parameter in the primary constructor of an immutable _case_ class

纵饮孤独 提交于 2019-12-06 15:04:08
I have normal Scala class I am wanting to refactor to become an immutable case class. As I'm needing the class to be well-behaved in Set operations, I want all the Scala compiler automatically generated methods provided on a case class. IOW, I am wanting to avoid having to write these various methods; equals , hashCode , toString , etc., as that is very error prone. And I am needing to do this for a raft of classes, so I need a general solution, not just a specific solution anomalous quick fix or hack. Here's the class with which I am working: class Node(val identity: String, childrenArg: List

Lift Case Class Exceeding 22 Arguments

☆樱花仙子☆ 提交于 2019-12-06 12:18:56
问题 Using Lift, I'm trying to "extract" (get a case class representation of) my JSON. val json: JValue = getJson() case class BigObj(name: String, age: Int, ...) json.extract[BigObj] When using more than 22 arguments, I get a JVM run-time exception that case classes cannot exceed 22 arguments. How can I work around this limit? 回答1: As has been mentioned, you can't overcome that limit with a case class explicitly, however you can use the extract method without a case class. See this example, which

What is *so* wrong with case class inheritance?

馋奶兔 提交于 2019-12-06 09:56:45
问题 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 ? 回答1: One word: equality case classes come with a supplied implementation of equals and hashCode . The equivalence relation, known as equals

Scala traits using generics and case class methods

て烟熏妆下的殇ゞ 提交于 2019-12-06 09:18:29
I have the following situation/code; trait Model { def myField: String } case class MyModel(myField: String) extends Model In the traditional model of creating DAOs for my model classes I want to create a DAO trait that contains some generic CRUD operations. NOTE...the persistence framework doesn't matter here...the question is around using case class methods within a trait that is using generics. With that said I want to create the following trait; trait DAO[M <: Model] { def insert(model: M): M = { ... do work m.copy(myField="someval") } } In this case the code does not compile because

Simple Iteration over case class fields

微笑、不失礼 提交于 2019-12-05 22:28:24
问题 I'm trying to write a generic method to iterate over a case class's fields : case class PriceMove(price: Double, delta: Double) def log(pm : PriceMove) { info("price -> " + price + " delta -> " + delta)} I need to make log able to handle any case class. What needs to be the argument type for log to handle case classes only and the actual generic field iteration code? 回答1: Okay, considering the two questions I attached to the question, here is what I'd use: object Implicits { implicit class

Handling case classes in twitter chill (Scala interface to Kryo)?

99封情书 提交于 2019-12-05 05:22:54
Twitter-chill looks like a good solution to the problem of how to serialize efficiently in Scala without excessive boilerplate. However, I don't see any evidence of how they handle case classes. Does this just work automatically or does something need to be done (e.g. creating a zero-arg constructor)? I have some experience with the WireFormat serialization mechanism built into Scoobi, which is a Scala Hadoop wrapper similar to Scalding. They have serializers for case classes up to 22 arguments that use the apply and unapply methods and do type matching on the arguments to these functions to

Tupled method for case class having a type parameter

自闭症网瘾萝莉.ら 提交于 2019-12-05 05:17:26
When having a case class with a type parameter, I don't see how I can call the tupled method. It seems to be find with apply and unapply . scala> case class Foo[T](x:T, y:T) defined class Foo scala> Foo.apply[Int] _ res1: (Int, Int) => Foo[Int] = <function2> scala> Foo.unapply[Int] _ res2: Foo[Int] => Option[(Int, Int)] = <function1> scala> Foo.tupled[Int] _ <console>:10: error: value tupled is not a member of object Foo Foo.tupled[Int] _ ^ Any idea on what's going on? tl;dr Companion objects of case classes cannot extend FunctionN (which defines tupled , with N >= 2) when they have type