case-class

DSL in scala using case classes

孤街浪徒 提交于 2019-12-05 02:52:05
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 20. I want to avoid redudancy of code I created a DSL using reflection so that we don't need to add

Modifying case class constructor parameter before setting value

China☆狼群 提交于 2019-12-05 01:33:30
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 either. Of course instead of apply() I could just have the private constructor and a factory method, but

Difference between home made extractor and case class extractor

懵懂的女人 提交于 2019-12-05 00:43:32
问题 According to the scala specification, the extractor built by case classes is the following (scala specification §5.3.2): def unapply[tps](x: c[tps]) = if (x eq null) scala.None else scala.Some(x.xs11, ..., x.xs1k) For implementation reasons, I want to be able to mimic the behavior of this extractor on a non-case class. However, my implementation fails to reproduce the same behavior. Here is an example of the difference i have: trait A sealed trait B[X <: A]{ val x: X } case class C[X <: A](x:

How to avoid scala's case class default toString function being overridden?

允我心安 提交于 2019-12-04 17:18:38
问题 Scala case class has a default toString function. But when this case class extends a trait with an existing toString() function, it will be rendered useless. How can I prevent this situation? 回答1: OK here is the easist answer: override def toString = ScalaRunTime._toString(this) end of story:) 回答2: Here's a workaround I think may work, it may be too much ceremony, you decide. It involves a trait . trait StandardToString { this:Product => override def toString = productPrefix + productIterator

Scala: order of definition for companion object vs case class

情到浓时终转凉″ 提交于 2019-12-04 16:11:49
问题 In Scala 2.9.1 I get the following behavior: class Foo { case class X() object X // this compiles def bar() { object Y // this compiles case class Y() case class Z() object Z // won't compile (see below) } } The compiler complains for Object Z : error: Z is already defined as (compiler-generated) case class companion object Z It looks as if it is not permissible to define a companion object for a case class after the case class definition if they are within a function definition. Is this a

What is *so* wrong with case class inheritance?

那年仲夏 提交于 2019-12-04 16:05:51
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

Slick 2.10-RC1, Scala 2.11.x, bypassing 22 arity limit with case class (heterogenous)

故事扮演 提交于 2019-12-04 12:18:26
问题 I am having issues in mapping a Table that has > 22 columns specifically into a case class , assuming you have the following code import slick.driver.PostgresDriver import scala.slick.collection.heterogenous._ import syntax._ import shapeless.Generic case class TwentyThreeCaseClass( val id:Option[Long], val one:String, val two:String, val three:String, val four:String, val five:String, val six:String, val seven:String, val eight:String, val nine:String, val ten:String, val eleven:String, val

Will the var members in case class affect case class's equality?

半世苍凉 提交于 2019-12-04 04:11:22
I have made heavy use of case classes in my code, replying on the underlying equality definitions of case class to behave correctly. Then now I found that I need to add another field member to a case class. So if I add a var field member in case class, will it mess up the equality attributes for the case class? If 1 is yes, then what if I just change the var field value once, after that, no any reassignment will happen, before the case class goes into any collections or do equality comparison, will that still mess up the equality behaviors? Case class equality is based solely on its primary

Equals for case class with floating point fields

拟墨画扇 提交于 2019-12-03 20:10:23
Is it ok, to create case classes with floating point fields, like: case class SomeClass(a:Double, b:Double) I guess auto generated equal method won't work in this case. Is overriding equals the best solution? EDIT: if overriding equals is the way to go, I would like to avoid hardcoding epsilon ( where epsilon is defined like => |this.a-a|< epsilon). This won't compile: case class SomeClass(a:Double, b:Double, implicit epsilon:Double) I am looking for a way to pass epsilon without passing concert value each time (some "implicit" magic). I have also follow up more general question, how would you

In Scala, how can I programmatically determine the name of the fields of a case class?

徘徊边缘 提交于 2019-12-03 12:52:39
问题 In Scala, suppose I have a case class like this: case class Sample(myInt: Int, myString: String) Is there a way for me to obtain a Seq[(String, Class[_])] , or better yet, Seq[(String, Manifest)] , describing the case class's parameters? 回答1: It's me again (two years later). Here's a different, different solution using Scala reflection. It is inspired by a blog post, which was itself inspired by a Stack Overflow exchange. The solution below is specialized to the original poster's question